Skip to main content

lender/adapters/
map_into_iter.rs

1use core::{iter::FusedIterator, marker::PhantomData};
2
3use crate::{DoubleEndedLender, ExactSizeLender, FusedLender, Lend, Lender};
4
5/// An iterator that maps each element of the underlying lender into an owned
6/// value.
7///
8/// This `struct` is created by the
9/// [`map_into_iter()`](crate::Lender::map_into_iter) method on [`Lender`].
10#[derive(Clone)]
11#[must_use = "iterators are lazy and do nothing unless consumed"]
12pub struct MapIntoIter<L, O, F> {
13    pub(crate) lender: L,
14    pub(crate) f: F,
15    pub(crate) _marker: PhantomData<fn() -> O>,
16}
17
18impl<L, O, F> MapIntoIter<L, O, F> {
19    #[inline(always)]
20    pub(crate) fn new(lender: L, f: F) -> MapIntoIter<L, O, F> {
21        MapIntoIter {
22            lender,
23            f,
24            _marker: PhantomData,
25        }
26    }
27
28    /// Returns the inner lender.
29    #[inline(always)]
30    pub fn into_inner(self) -> L {
31        self.lender
32    }
33
34    /// Returns the inner lender and the mapping function.
35    #[inline(always)]
36    pub fn into_parts(self) -> (L, F) {
37        (self.lender, self.f)
38    }
39}
40
41impl<L: core::fmt::Debug, O, F> core::fmt::Debug for MapIntoIter<L, O, F> {
42    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43        f.debug_struct("MapIntoIter")
44            .field("lender", &self.lender)
45            .finish_non_exhaustive()
46    }
47}
48
49impl<L: Lender, O, F: FnMut(Lend<'_, L>) -> O> Iterator for MapIntoIter<L, O, F> {
50    type Item = O;
51    #[inline]
52    fn next(&mut self) -> Option<O> {
53        self.lender.next().map(&mut self.f)
54    }
55
56    #[inline(always)]
57    fn size_hint(&self) -> (usize, Option<usize>) {
58        self.lender.size_hint()
59    }
60
61    #[inline]
62    fn nth(&mut self, n: usize) -> Option<O> {
63        self.lender.nth(n).map(&mut self.f)
64    }
65
66    #[inline]
67    fn fold<B, G>(self, init: B, mut g: G) -> B
68    where
69        G: FnMut(B, Self::Item) -> B,
70    {
71        let mut f = self.f;
72        self.lender.fold(init, |acc, x| g(acc, f(x)))
73    }
74}
75
76impl<L: DoubleEndedLender, O, F: FnMut(Lend<'_, L>) -> O> DoubleEndedIterator
77    for MapIntoIter<L, O, F>
78{
79    #[inline]
80    fn next_back(&mut self) -> Option<O> {
81        self.lender.next_back().map(&mut self.f)
82    }
83
84    #[inline]
85    fn nth_back(&mut self, n: usize) -> Option<O> {
86        self.lender.nth_back(n).map(&mut self.f)
87    }
88
89    #[inline]
90    fn rfold<B, G>(self, init: B, mut g: G) -> B
91    where
92        G: FnMut(B, Self::Item) -> B,
93    {
94        let mut f = self.f;
95        self.lender.rfold(init, |acc, x| g(acc, f(x)))
96    }
97}
98
99impl<L: ExactSizeLender, O, F: FnMut(Lend<'_, L>) -> O> ExactSizeIterator for MapIntoIter<L, O, F> {
100    #[inline(always)]
101    fn len(&self) -> usize {
102        self.lender.len()
103    }
104}
105
106impl<L: FusedLender, O, F: FnMut(Lend<'_, L>) -> O> FusedIterator for MapIntoIter<L, O, F> {}