pub(crate) struct ReversibleIterable { pub(crate) it: It, pub(crate) reverse: bool, } impl ReversibleIterable { pub(crate) fn new(it: T, reverse: bool) -> Self { Self { it, reverse } } } impl ReversibleIterable where It: Iterator, { pub(crate) fn find_single_ended(mut self, pred: F) -> Option where F: FnMut(&Item) -> bool, { if self.reverse { self.it.filter(pred).last() } else { self.it.find(pred) } } } impl ReversibleIterable where It: DoubleEndedIterator, { pub(crate) fn find(mut self, mut pred: F) -> Option where F: FnMut(&Item) -> bool, { if self.reverse { self.it.rfind(|x| pred(x)) } else { self.it.find(|x| pred(x)) } } }