2 use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable};
5 /// An iterator that calls a function with a reference to each element before
8 /// This `struct` is created by the [`inspect`] method on [`Iterator`]. See its
9 /// documentation for more.
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")]
16 pub struct Inspect<I, F> {
20 impl<I, F> Inspect<I, F> {
21 pub(in crate::iter) fn new(iter: I, f: F) -> Inspect<I, F> {
26 #[stable(feature = "core_impl_debug", since = "1.9.0")]
27 impl<I: fmt::Debug, F> fmt::Debug for Inspect<I, F> {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 f.debug_struct("Inspect").field("iter", &self.iter).finish()
33 impl<I: Iterator, F> Inspect<I, F>
38 fn do_inspect(&mut self, elt: Option<I::Item>) -> Option<I::Item> {
39 if let Some(ref a) = elt {
47 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 {
57 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 {
67 #[stable(feature = "rust1", since = "1.0.0")]
68 impl<I: Iterator, F> Iterator for Inspect<I, F>
75 fn next(&mut self) -> Option<I::Item> {
76 let next = self.iter.next();
81 fn size_hint(&self) -> (usize, Option<usize>) {
86 fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
89 Fold: FnMut(Acc, Self::Item) -> R,
92 self.iter.try_fold(init, inspect_try_fold(&mut self.f, fold))
96 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
98 Fold: FnMut(Acc, Self::Item) -> Acc,
100 self.iter.fold(init, inspect_fold(self.f, fold))
104 #[stable(feature = "rust1", since = "1.0.0")]
105 impl<I: DoubleEndedIterator, F> DoubleEndedIterator for Inspect<I, F>
110 fn next_back(&mut self) -> Option<I::Item> {
111 let next = self.iter.next_back();
112 self.do_inspect(next)
116 fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
119 Fold: FnMut(Acc, Self::Item) -> R,
122 self.iter.try_rfold(init, inspect_try_fold(&mut self.f, fold))
126 fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
128 Fold: FnMut(Acc, Self::Item) -> Acc,
130 self.iter.rfold(init, inspect_fold(self.f, fold))
134 #[stable(feature = "rust1", since = "1.0.0")]
135 impl<I: ExactSizeIterator, F> ExactSizeIterator for Inspect<I, F>
139 fn len(&self) -> usize {
143 fn is_empty(&self) -> bool {
148 #[stable(feature = "fused", since = "1.26.0")]
149 impl<I: FusedIterator, F> FusedIterator for Inspect<I, F> where F: FnMut(&I::Item) {}
151 #[unstable(issue = "none", feature = "inplace_iteration")]
152 unsafe impl<S: Iterator, I: Iterator, F> SourceIter for Inspect<I, F>
155 I: SourceIter<Source = S>,
160 unsafe fn as_inner(&mut self) -> &mut S {
161 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
162 unsafe { SourceIter::as_inner(&mut self.iter) }
166 #[unstable(issue = "none", feature = "inplace_iteration")]
167 unsafe impl<I: InPlaceIterable, F> InPlaceIterable for Inspect<I, F> where F: FnMut(&I::Item) {}