Skip to main content

core/iter/adapters/
map.rs

1use crate::fmt;
2use crate::iter::adapters::zip::try_get_unchecked;
3use crate::iter::adapters::{SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
4use crate::iter::{FusedIterator, InPlaceIterable, TrustedFused, TrustedLen};
5use crate::num::NonZero;
6use crate::ops::Try;
7
8/// An iterator that maps the values of `iter` with `f`.
9///
10/// This `struct` is created by the [`map`] method on [`Iterator`]. See its
11/// documentation for more.
12///
13/// [`map`]: Iterator::map
14/// [`Iterator`]: trait.Iterator.html
15///
16/// # Notes about side effects
17///
18/// The [`map`] iterator implements [`DoubleEndedIterator`], meaning that
19/// you can also [`map`] backwards:
20///
21/// ```rust
22/// let v: Vec<i32> = [1, 2, 3].into_iter().map(|x| x + 1).rev().collect();
23///
24/// assert_eq!(v, [4, 3, 2]);
25/// ```
26///
27/// [`DoubleEndedIterator`]: trait.DoubleEndedIterator.html
28///
29/// But if your closure has state, iterating backwards may act in a way you do
30/// not expect. Let's go through an example. First, in the forward direction:
31///
32/// ```rust
33/// let mut c = 0;
34///
35/// for pair in ['a', 'b', 'c'].into_iter()
36///                                .map(|letter| { c += 1; (letter, c) }) {
37///     println!("{pair:?}");
38/// }
39/// ```
40///
41/// This will print `('a', 1), ('b', 2), ('c', 3)`.
42///
43/// Now consider this twist where we add a call to `rev`. This version will
44/// print `('c', 1), ('b', 2), ('a', 3)`. Note that the letters are reversed,
45/// but the values of the counter still go in order. This is because `map()` is
46/// still being called lazily on each item, but we are popping items off the
47/// back of the vector now, instead of shifting them from the front.
48///
49/// ```rust
50/// let mut c = 0;
51///
52/// for pair in ['a', 'b', 'c'].into_iter()
53///                                .map(|letter| { c += 1; (letter, c) })
54///                                .rev() {
55///     println!("{pair:?}");
56/// }
57/// ```
58#[must_use = "iterators are lazy and do nothing unless consumed"]
59#[stable(feature = "rust1", since = "1.0.0")]
60#[derive(Clone)]
61pub struct Map<I, F> {
62    // Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods
63    pub(crate) iter: I,
64    f: F,
65}
66
67impl<I, F> Map<I, F> {
68    pub(in crate::iter) const fn new(iter: I, f: F) -> Map<I, F> {
69        Map { iter, f }
70    }
71
72    pub(crate) fn into_inner(self) -> I {
73        self.iter
74    }
75}
76
77#[stable(feature = "core_impl_debug", since = "1.9.0")]
78impl<I: fmt::Debug, F> fmt::Debug for Map<I, F> {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        f.debug_struct("Map").field("iter", &self.iter).finish()
81    }
82}
83
84fn map_fold<T, B, Acc>(
85    mut f: impl FnMut(T) -> B,
86    mut g: impl FnMut(Acc, B) -> Acc,
87) -> impl FnMut(Acc, T) -> Acc {
88    move |acc, elt| g(acc, f(elt))
89}
90
91fn map_try_fold<'a, T, B, Acc, R>(
92    f: &'a mut impl FnMut(T) -> B,
93    mut g: impl FnMut(Acc, B) -> R + 'a,
94) -> impl FnMut(Acc, T) -> R + 'a {
95    move |acc, elt| g(acc, f(elt))
96}
97
98#[stable(feature = "rust1", since = "1.0.0")]
99impl<B, I: Iterator, F> Iterator for Map<I, F>
100where
101    F: FnMut(I::Item) -> B,
102{
103    type Item = B;
104
105    #[inline]
106    fn next(&mut self) -> Option<B> {
107        self.iter.next().map(&mut self.f)
108    }
109
110    #[inline]
111    fn size_hint(&self) -> (usize, Option<usize>) {
112        self.iter.size_hint()
113    }
114
115    #[inline]
116    fn try_fold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
117    where
118        Self: Sized,
119        G: FnMut(Acc, Self::Item) -> R,
120        R: Try<Output = Acc>,
121    {
122        self.iter.try_fold(init, map_try_fold(&mut self.f, g))
123    }
124
125    #[inline]
126    fn fold<Acc, G>(self, init: Acc, g: G) -> Acc
127    where
128        G: FnMut(Acc, Self::Item) -> Acc,
129    {
130        self.iter.fold(init, map_fold(self.f, g))
131    }
132
133    #[inline]
134    unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> B
135    where
136        Self: TrustedRandomAccessNoCoerce,
137    {
138        // SAFETY: the caller must uphold the contract for
139        // `Iterator::__iterator_get_unchecked`.
140        unsafe { (self.f)(try_get_unchecked(&mut self.iter, idx)) }
141    }
142}
143
144#[stable(feature = "rust1", since = "1.0.0")]
145impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F>
146where
147    F: FnMut(I::Item) -> B,
148{
149    #[inline]
150    fn next_back(&mut self) -> Option<B> {
151        self.iter.next_back().map(&mut self.f)
152    }
153
154    #[inline]
155    fn try_rfold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
156    where
157        Self: Sized,
158        G: FnMut(Acc, Self::Item) -> R,
159        R: Try<Output = Acc>,
160    {
161        self.iter.try_rfold(init, map_try_fold(&mut self.f, g))
162    }
163
164    #[inline]
165    fn rfold<Acc, G>(self, init: Acc, g: G) -> Acc
166    where
167        G: FnMut(Acc, Self::Item) -> Acc,
168    {
169        self.iter.rfold(init, map_fold(self.f, g))
170    }
171}
172
173#[stable(feature = "rust1", since = "1.0.0")]
174impl<B, I: ExactSizeIterator, F> ExactSizeIterator for Map<I, F>
175where
176    F: FnMut(I::Item) -> B,
177{
178    fn len(&self) -> usize {
179        self.iter.len()
180    }
181
182    fn is_empty(&self) -> bool {
183        self.iter.is_empty()
184    }
185}
186
187#[stable(feature = "fused", since = "1.26.0")]
188impl<B, I: FusedIterator, F> FusedIterator for Map<I, F> where F: FnMut(I::Item) -> B {}
189
190#[unstable(issue = "none", feature = "trusted_fused")]
191unsafe impl<I: TrustedFused, F> TrustedFused for Map<I, F> {}
192
193#[unstable(feature = "trusted_len", issue = "37572")]
194unsafe impl<B, I, F> TrustedLen for Map<I, F>
195where
196    I: TrustedLen,
197    F: FnMut(I::Item) -> B,
198{
199}
200
201#[doc(hidden)]
202#[unstable(feature = "trusted_random_access", issue = "none")]
203unsafe impl<I, F> TrustedRandomAccess for Map<I, F> where I: TrustedRandomAccess {}
204
205#[doc(hidden)]
206#[unstable(feature = "trusted_random_access", issue = "none")]
207unsafe impl<I, F> TrustedRandomAccessNoCoerce for Map<I, F>
208where
209    I: TrustedRandomAccessNoCoerce,
210{
211    const MAY_HAVE_SIDE_EFFECT: bool = true;
212}
213
214#[unstable(issue = "none", feature = "inplace_iteration")]
215unsafe impl<I, F> SourceIter for Map<I, F>
216where
217    I: SourceIter,
218{
219    type Source = I::Source;
220
221    #[inline]
222    unsafe fn as_inner(&mut self) -> &mut I::Source {
223        // SAFETY: unsafe function forwarding to unsafe function with the same requirements
224        unsafe { SourceIter::as_inner(&mut self.iter) }
225    }
226}
227
228#[unstable(issue = "none", feature = "inplace_iteration")]
229unsafe impl<I: InPlaceIterable, F> InPlaceIterable for Map<I, F> {
230    const EXPAND_BY: Option<NonZero<usize>> = I::EXPAND_BY;
231    const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY;
232}