1 use crate::fmt;
2 use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable};
3 use crate::ops::Try;
4 
5 /// An iterator that calls a function with a reference to each element before
6 /// yielding it.
7 ///
8 /// This `struct` is created by the [`inspect`] method on [`Iterator`]. See its
9 /// documentation for more.
10 ///
11 /// [`inspect`]: Iterator::inspect
12 /// [`Iterator`]: trait.Iterator.html
13 #[must_use = "iterators are lazy and do nothing unless consumed"]
14 #[stable(feature = "rust1", since = "1.0.0")]
15 #[derive(Clone)]
16 pub struct Inspect<I, F> {
17     iter: I,
18     f: F,
19 }
20 impl<I, F> Inspect<I, F> {
new(iter: I, f: F) -> Inspect<I, F>21     pub(in crate::iter) fn new(iter: I, f: F) -> Inspect<I, F> {
22         Inspect { iter, f }
23     }
24 }
25 
26 #[stable(feature = "core_impl_debug", since = "1.9.0")]
27 impl<I: fmt::Debug, F> fmt::Debug for Inspect<I, F> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result28     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29         f.debug_struct("Inspect").field("iter", &self.iter).finish()
30     }
31 }
32 
33 impl<I: Iterator, F> Inspect<I, F>
34 where
35     F: FnMut(&I::Item),
36 {
37     #[inline]
do_inspect(&mut self, elt: Option<I::Item>) -> Option<I::Item>38     fn do_inspect(&mut self, elt: Option<I::Item>) -> Option<I::Item> {
39         if let Some(ref a) = elt {
40             (self.f)(a);
41         }
42 
43         elt
44     }
45 }
46 
inspect_fold<T, Acc>( mut f: impl FnMut(&T), mut fold: impl FnMut(Acc, T) -> Acc, ) -> impl FnMut(Acc, T) -> Acc47 fn inspect_fold<T, Acc>(
48     mut f: impl FnMut(&T),
49     mut fold: impl FnMut(Acc, T) -> Acc,
50 ) -> impl FnMut(Acc, T) -> Acc {
51     move |acc, item| {
52         f(&item);
53         fold(acc, item)
54     }
55 }
56 
inspect_try_fold<'a, T, Acc, R>( f: &'a mut impl FnMut(&T), mut fold: impl FnMut(Acc, T) -> R + 'a, ) -> impl FnMut(Acc, T) -> R + 'a57 fn inspect_try_fold<'a, T, Acc, R>(
58     f: &'a mut impl FnMut(&T),
59     mut fold: impl FnMut(Acc, T) -> R + 'a,
60 ) -> impl FnMut(Acc, T) -> R + 'a {
61     move |acc, item| {
62         f(&item);
63         fold(acc, item)
64     }
65 }
66 
67 #[stable(feature = "rust1", since = "1.0.0")]
68 impl<I: Iterator, F> Iterator for Inspect<I, F>
69 where
70     F: FnMut(&I::Item),
71 {
72     type Item = I::Item;
73 
74     #[inline]
next(&mut self) -> Option<I::Item>75     fn next(&mut self) -> Option<I::Item> {
76         let next = self.iter.next();
77         self.do_inspect(next)
78     }
79 
80     #[inline]
size_hint(&self) -> (usize, Option<usize>)81     fn size_hint(&self) -> (usize, Option<usize>) {
82         self.iter.size_hint()
83     }
84 
85     #[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>,86     fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
87     where
88         Self: Sized,
89         Fold: FnMut(Acc, Self::Item) -> R,
90         R: Try<Output = Acc>,
91     {
92         self.iter.try_fold(init, inspect_try_fold(&mut self.f, fold))
93     }
94 
95     #[inline]
fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc where Fold: FnMut(Acc, Self::Item) -> Acc,96     fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
97     where
98         Fold: FnMut(Acc, Self::Item) -> Acc,
99     {
100         self.iter.fold(init, inspect_fold(self.f, fold))
101     }
102 }
103 
104 #[stable(feature = "rust1", since = "1.0.0")]
105 impl<I: DoubleEndedIterator, F> DoubleEndedIterator for Inspect<I, F>
106 where
107     F: FnMut(&I::Item),
108 {
109     #[inline]
next_back(&mut self) -> Option<I::Item>110     fn next_back(&mut self) -> Option<I::Item> {
111         let next = self.iter.next_back();
112         self.do_inspect(next)
113     }
114 
115     #[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>,116     fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
117     where
118         Self: Sized,
119         Fold: FnMut(Acc, Self::Item) -> R,
120         R: Try<Output = Acc>,
121     {
122         self.iter.try_rfold(init, inspect_try_fold(&mut self.f, fold))
123     }
124 
125     #[inline]
rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc where Fold: FnMut(Acc, Self::Item) -> Acc,126     fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
127     where
128         Fold: FnMut(Acc, Self::Item) -> Acc,
129     {
130         self.iter.rfold(init, inspect_fold(self.f, fold))
131     }
132 }
133 
134 #[stable(feature = "rust1", since = "1.0.0")]
135 impl<I: ExactSizeIterator, F> ExactSizeIterator for Inspect<I, F>
136 where
137     F: FnMut(&I::Item),
138 {
len(&self) -> usize139     fn len(&self) -> usize {
140         self.iter.len()
141     }
142 
is_empty(&self) -> bool143     fn is_empty(&self) -> bool {
144         self.iter.is_empty()
145     }
146 }
147 
148 #[stable(feature = "fused", since = "1.26.0")]
149 impl<I: FusedIterator, F> FusedIterator for Inspect<I, F> where F: FnMut(&I::Item) {}
150 
151 #[unstable(issue = "none", feature = "inplace_iteration")]
152 unsafe impl<I, F> SourceIter for Inspect<I, F>
153 where
154     I: SourceIter,
155 {
156     type Source = I::Source;
157 
158     #[inline]
as_inner(&mut self) -> &mut I::Source159     unsafe fn as_inner(&mut self) -> &mut I::Source {
160         // SAFETY: unsafe function forwarding to unsafe function with the same requirements
161         unsafe { SourceIter::as_inner(&mut self.iter) }
162     }
163 }
164 
165 #[unstable(issue = "none", feature = "inplace_iteration")]
166 unsafe impl<I: InPlaceIterable, F> InPlaceIterable for Inspect<I, F> where F: FnMut(&I::Item) {}
167