petgraph/
iter_utils.rs

1#[cfg_attr(not(feature = "stable_graph"), allow(dead_code))]
2pub trait IterUtilsExt: Iterator {
3    /// Return the first element that maps to `Some(_)`, or None if the iterator
4    /// was exhausted.
5    fn ex_find_map<F, R>(&mut self, mut f: F) -> Option<R>
6    where
7        F: FnMut(Self::Item) -> Option<R>,
8    {
9        for elt in self {
10            if let result @ Some(_) = f(elt) {
11                return result;
12            }
13        }
14        None
15    }
16
17    /// Return the last element from the back that maps to `Some(_)`, or
18    /// None if the iterator was exhausted.
19    fn ex_rfind_map<F, R>(&mut self, mut f: F) -> Option<R>
20    where
21        F: FnMut(Self::Item) -> Option<R>,
22        Self: DoubleEndedIterator,
23    {
24        while let Some(elt) = self.next_back() {
25            if let result @ Some(_) = f(elt) {
26                return result;
27            }
28        }
29        None
30    }
31}
32
33impl<I> IterUtilsExt for I where I: Iterator {}