1 use crate::fmt;
2 use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable};
3 use crate::ops::{ControlFlow, Try};
4 
5 /// An iterator that uses `f` to both filter and map elements from `iter`.
6 ///
7 /// This `struct` is created by the [`filter_map`] method on [`Iterator`]. See its
8 /// documentation for more.
9 ///
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")]
14 #[derive(Clone)]
15 pub struct FilterMap<I, F> {
16     iter: I,
17     f: F,
18 }
19 impl<I, F> FilterMap<I, F> {
new(iter: I, f: F) -> FilterMap<I, F>20     pub(in crate::iter) fn new(iter: I, f: F) -> FilterMap<I, F> {
21         FilterMap { iter, f }
22     }
23 }
24 
25 #[stable(feature = "core_impl_debug", since = "1.9.0")]
26 impl<I: fmt::Debug, F> fmt::Debug for FilterMap<I, F> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result27     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28         f.debug_struct("FilterMap").field("iter", &self.iter).finish()
29     }
30 }
31 
filter_map_fold<T, B, Acc>( mut f: impl FnMut(T) -> Option<B>, mut fold: impl FnMut(Acc, B) -> Acc, ) -> impl FnMut(Acc, T) -> Acc32 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),
38         None => acc,
39     }
40 }
41 
filter_map_try_fold<'a, T, B, Acc, R: Try<Output = Acc>>( f: &'a mut impl FnMut(T) -> Option<B>, mut fold: impl FnMut(Acc, B) -> R + 'a, ) -> impl FnMut(Acc, T) -> R + 'a42 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),
48         None => try { acc },
49     }
50 }
51 
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
54 where
55     F: FnMut(I::Item) -> Option<B>,
56 {
57     type Item = B;
58 
59     #[inline]
next(&mut self) -> Option<B>60     fn next(&mut self) -> Option<B> {
61         self.iter.find_map(&mut self.f)
62     }
63 
64     #[inline]
size_hint(&self) -> (usize, Option<usize>)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
68     }
69 
70     #[inline]
try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Output = Acc>,71     fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
72     where
73         Self: Sized,
74         Fold: FnMut(Acc, Self::Item) -> R,
75         R: Try<Output = Acc>,
76     {
77         self.iter.try_fold(init, filter_map_try_fold(&mut self.f, fold))
78     }
79 
80     #[inline]
fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc where Fold: FnMut(Acc, Self::Item) -> Acc,81     fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
82     where
83         Fold: FnMut(Acc, Self::Item) -> Acc,
84     {
85         self.iter.fold(init, filter_map_fold(self.f, fold))
86     }
87 }
88 
89 #[stable(feature = "rust1", since = "1.0.0")]
90 impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for FilterMap<I, F>
91 where
92     F: FnMut(I::Item) -> Option<B>,
93 {
94     #[inline]
next_back(&mut self) -> Option<B>95     fn next_back(&mut self) -> Option<B> {
96         #[inline]
97         fn find<T, 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,
103             }
104         }
105 
106         self.iter.try_rfold((), find(&mut self.f)).break_value()
107     }
108 
109     #[inline]
try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Output = Acc>,110     fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
111     where
112         Self: Sized,
113         Fold: FnMut(Acc, Self::Item) -> R,
114         R: Try<Output = Acc>,
115     {
116         self.iter.try_rfold(init, filter_map_try_fold(&mut self.f, fold))
117     }
118 
119     #[inline]
rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc where Fold: FnMut(Acc, Self::Item) -> Acc,120     fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
121     where
122         Fold: FnMut(Acc, Self::Item) -> Acc,
123     {
124         self.iter.rfold(init, filter_map_fold(self.f, fold))
125     }
126 }
127 
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> {}
130 
131 #[unstable(issue = "none", feature = "inplace_iteration")]
132 unsafe impl<I, F> SourceIter for FilterMap<I, F>
133 where
134     I: SourceIter,
135 {
136     type Source = I::Source;
137 
138     #[inline]
as_inner(&mut self) -> &mut I::Source139     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) }
142     }
143 }
144 
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>
148 {
149 }
150