pure_iter/adapter_impls/
map.rs1use core::iter::FusedIterator;
2
3#[derive(Debug, Clone)]
4pub struct PureMap<I: Iterator, F> {
5 iter: I,
6 f: F
7}
8
9impl<I: Iterator, F> PureMap<I, F> {
10 pub fn new(iter: I, f: F) -> Self {
11 Self { iter, f }
12 }
13}
14
15impl<I, F1, U> Iterator for PureMap<I, F1>
16where I: Iterator,
17 F1: Fn(I::Item) -> U,
18{
19 type Item = U;
20
21 fn next(&mut self) -> Option<Self::Item> {
22 self.iter.next().map(&self.f)
23 }
24
25 fn nth(&mut self, n: usize) -> Option<Self::Item> {
26 self.iter.nth(n).map(&self.f)
27 }
28
29 fn size_hint(&self) -> (usize, Option<usize>) {
30 self.iter.size_hint()
31 }
32
33 fn count(self) -> usize
34 where Self: Sized,
35 {
36 self.iter.count()
37 }
38
39 fn last(self) -> Option<Self::Item>
40 where Self: Sized,
41 {
42 self.iter.last().map(self.f)
43 }
44
45 fn fold<B, F>(self, init: B, mut f: F) -> B
46 where Self: Sized,
47 F: FnMut(B, Self::Item) -> B,
48 {
49 self.iter.fold(init, |acc, ele| {
50 f(acc, (self.f)(ele))
51 })
52 }
53
54 #[cfg(feature = "unstable")]
55 fn advance_by(&mut self, n: usize) -> Result<(), core::num::NonZero<usize>> {
56 self.iter.advance_by(n)
57 }
58
59 #[cfg(feature = "unstable")]
60 fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R
61 where Self: Sized,
62 F: FnMut(B, Self::Item) -> R,
63 R: core::ops::Try<Output = B>,
64 {
65 self.iter.try_fold(init, |acc, ele| {
66 f(acc, (self.f)(ele))
67 })
68 }
69}
70
71impl<I, F1, U> DoubleEndedIterator for PureMap<I, F1>
72where I: DoubleEndedIterator,
73 F1: Fn(I::Item) -> U,
74{
75 fn next_back(&mut self) -> Option<Self::Item> {
76 self.iter.next_back().map(&self.f)
77 }
78
79 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
80 self.iter.nth_back(n).map(&self.f)
81 }
82
83 fn rfold<B, F>(self, init: B, mut f: F) -> B
84 where Self: Sized,
85 F: FnMut(B, Self::Item) -> B,
86 {
87 self.iter.rfold(init, |acc, ele| {
88 f(acc, (self.f)(ele))
89 })
90 }
91
92 #[cfg(feature = "unstable")]
93 fn advance_back_by(&mut self, n: usize) -> Result<(), core::num::NonZero<usize>> {
94 self.iter.advance_back_by(n)
95 }
96
97 #[cfg(feature = "unstable")]
98 fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
99 where Self: Sized,
100 F: FnMut(B, Self::Item) -> R,
101 R: core::ops::Try<Output = B>,
102 {
103 self.iter.try_rfold(init, |acc, ele| {
104 f(acc, (self.f)(ele))
105 })
106 }
107}
108
109impl<I, F1, U> ExactSizeIterator for PureMap<I, F1>
110where I: ExactSizeIterator,
111 F1: Fn(I::Item) -> U,
112{
113}
114
115impl<I, F1, U> FusedIterator for PureMap<I, F1>
116where I: FusedIterator,
117 F1: Fn(I::Item) -> U,
118{
119}