1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{LendingIterator, SingleArgFnMut, SingleArgFnOnce};
use core::fmt;

/// A lending iterator that maps the elements of `iter` with `f`.
///
/// This `struct` is created by the [`map`] method on [`LendingIterator`]. See
/// its documentation for more.
///
/// [`LendingIterator`]: crate::LendingIterator
/// [`map`]: crate::LendingIterator::map
#[derive(Clone)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Map<I, F> {
    iter: I,
    f: F,
}

impl<I, F> Map<I, F> {
    pub(crate) fn new(iter: I, f: F) -> Self {
        Self { iter, f }
    }
}

impl<I: fmt::Debug, F> fmt::Debug for Map<I, F> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Map").field("iter", &self.iter).finish_non_exhaustive()
    }
}

impl<I, F> LendingIterator for Map<I, F>
where
    I: LendingIterator,
    F: for<'a> SingleArgFnMut<I::Item<'a>>,
{
    type Item<'a> = <F as SingleArgFnOnce<I::Item<'a>>>::Output
        where
            Self: 'a;

    #[inline]
    fn next(&mut self) -> Option<Self::Item<'_>> {
        self.iter.next().map(&mut self.f)
    }
}

/// An iterator that maps the elements of `iter` with `f`.
///
/// This `struct` is created when [`IntoIterator::into_iter`] is called on [`Map`].
pub struct IntoIter<I, F> {
    iter: I,
    f: F,
}

impl<I, F, O> Iterator for IntoIter<I, F>
where
    I: LendingIterator,
    F: FnMut(I::Item<'_>) -> O,
{
    type Item = O;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(&mut self.f)
    }
}

impl<I, F, O> IntoIterator for Map<I, F>
where
    I: LendingIterator,
    F: FnMut(I::Item<'_>) -> O,
{
    type Item = O;
    type IntoIter = IntoIter<I, F>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            iter: self.iter,
            f: self.f,
        }
    }
}