2 use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable};
3 use crate::ops::{ControlFlow, Try};
5 /// An iterator that uses `f` to both filter and map elements from `iter`.
7 /// This `struct` is created by the [`filter_map`] method on [`Iterator`]. See its
8 /// documentation for more.
10 /// [`filter_map`]: Iterator::filter_map
11 /// [`Iterator`]: trait.Iterator.html
12 #[must_use = "iterators are lazy and do nothing unless consumed"]
13 #[stable(feature = "rust1", since = "1.0.0")]
15 pub struct FilterMap<I, F> {
19 impl<I, F> FilterMap<I, F> {
20 pub(in crate::iter) fn new(iter: I, f: F) -> FilterMap<I, F> {
25 #[stable(feature = "core_impl_debug", since = "1.9.0")]
26 impl<I: fmt::Debug, F> fmt::Debug for FilterMap<I, F> {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 f.debug_struct("FilterMap").field("iter", &self.iter).finish()
32 fn filter_map_fold<T, B, Acc>(
33 mut f: impl FnMut(T) -> Option<B>,
34 mut fold: impl FnMut(Acc, B) -> Acc,
35 ) -> impl FnMut(Acc, T) -> Acc {
36 move |acc, item| match f(item) {
37 Some(x) => fold(acc, x),
42 fn filter_map_try_fold<'a, T, B, Acc, R: Try<Output = Acc>>(
43 f: &'a mut impl FnMut(T) -> Option<B>,
44 mut fold: impl FnMut(Acc, B) -> R + 'a,
45 ) -> impl FnMut(Acc, T) -> R + 'a {
46 move |acc, item| match f(item) {
47 Some(x) => fold(acc, x),
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
55 F: FnMut(I::Item) -> Option<B>,
60 fn next(&mut self) -> Option<B> {
61 self.iter.find_map(&mut self.f)
65 fn size_hint(&self) -> (usize, Option<usize>) {
66 let (_, upper) = self.iter.size_hint();
67 (0, upper) // can't know a lower bound, due to the predicate
71 fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
74 Fold: FnMut(Acc, Self::Item) -> R,
77 self.iter.try_fold(init, filter_map_try_fold(&mut self.f, fold))
81 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
83 Fold: FnMut(Acc, Self::Item) -> Acc,
85 self.iter.fold(init, filter_map_fold(self.f, fold))
89 #[stable(feature = "rust1", since = "1.0.0")]
90 impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for FilterMap<I, F>
92 F: FnMut(I::Item) -> Option<B>,
95 fn next_back(&mut self) -> Option<B> {
98 f: &mut impl FnMut(T) -> Option<B>,
99 ) -> impl FnMut((), T) -> ControlFlow<B> + '_ {
100 move |(), x| match f(x) {
101 Some(x) => ControlFlow::Break(x),
102 None => ControlFlow::CONTINUE,
106 self.iter.try_rfold((), find(&mut self.f)).break_value()
110 fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
113 Fold: FnMut(Acc, Self::Item) -> R,
114 R: Try<Output = Acc>,
116 self.iter.try_rfold(init, filter_map_try_fold(&mut self.f, fold))
120 fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
122 Fold: FnMut(Acc, Self::Item) -> Acc,
124 self.iter.rfold(init, filter_map_fold(self.f, fold))
128 #[stable(feature = "fused", since = "1.26.0")]
129 impl<B, I: FusedIterator, F> FusedIterator for FilterMap<I, F> where F: FnMut(I::Item) -> Option<B> {}
131 #[unstable(issue = "none", feature = "inplace_iteration")]
132 unsafe impl<I, F> SourceIter for FilterMap<I, F>
136 type Source = I::Source;
139 unsafe fn as_inner(&mut self) -> &mut I::Source {
140 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
141 unsafe { SourceIter::as_inner(&mut self.iter) }
145 #[unstable(issue = "none", feature = "inplace_iteration")]
146 unsafe impl<B, I: InPlaceIterable, F> InPlaceIterable for FilterMap<I, F> where
147 F: FnMut(I::Item) -> Option<B>