bon_macros/util/
vec.rs

1use crate::util::prelude::*;
2
3pub(crate) trait VecExt<T> {
4    /// Remove all alements from the [`Vec`] that do not satisfy the predicate.
5    /// Also bail on the first error that the predicate returns.
6    fn try_retain_mut(&mut self, f: impl FnMut(&mut T) -> Result<bool>) -> Result;
7}
8
9impl<T> VecExt<T> for Vec<T> {
10    fn try_retain_mut(&mut self, mut try_predicate: impl FnMut(&mut T) -> Result<bool>) -> Result {
11        let mut i = 0;
12        while i < self.len() {
13            if try_predicate(&mut self[i])? {
14                i += 1;
15            } else {
16                self.remove(i);
17            }
18        }
19        Ok(())
20    }
21}