iter_flow/
map_ok.rs

1#[derive(Clone)]
2#[must_use = "iterators are lazy and do nothing unless consumed"]
3pub struct MapOkBy<I, F> {
4    iter: I,
5    f: F,
6}
7
8pub trait MapOkPredicate<T> {
9    type Out;
10    fn call(&mut self, t: T) -> Self::Out;
11}
12
13impl<I, F, T, E> Iterator for MapOkBy<I, F>
14where
15    I: Iterator<Item = Result<T, E>>,
16    F: MapOkPredicate<T>,
17{
18    type Item = Result<F::Out, E>;
19
20    fn next(&mut self) -> Option<Self::Item> {
21        self.iter.next().map(|e| e.map(|t| self.f.call(t)))
22    }
23
24    fn size_hint(&self) -> (usize, Option<usize>) {
25        self.iter.size_hint()
26    }
27
28    fn fold<Acc, Fold>(self, init: Acc, mut fold_f: Fold) -> Acc
29    where
30        Self: Sized,
31        Fold: FnMut(Acc, Self::Item) -> Acc,
32    {
33        let mut f = self.f;
34        self.iter.fold(init, move |acc, v| fold_f(acc, v.map(|t| f.call(t))))
35    }
36
37    fn collect<B: FromIterator<Self::Item>>(self) -> B
38    where
39        Self: Sized,
40    {
41        let mut f = self.f;
42        self.iter.map(move |v| v.map(|t| f.call(t))).collect()
43    }
44}
45
46/// An iterator adaptor that maps the inner value
47///
48/// See [`.map_ok()`](crate::Iterflow::map_ok) for more information.
49pub type MapOk<I, F> = MapOkBy<I, MapOkFn<F>>;
50
51impl<F, T, O> MapOkPredicate<T> for MapOkFn<F>
52where
53    F: FnMut(T) -> O,
54{
55    type Out = O;
56
57    fn call(&mut self, t: T) -> Self::Out {
58        self.0(t)
59    }
60}
61
62#[derive(Clone)]
63pub struct MapOkFn<F>(F);
64
65/// Create a new `MapOk`.
66pub fn map_ok<I, F, E, O>(iter: I, f: F) -> MapOk<I, F>
67where
68    I: Iterator,
69    F: FnMut(E) -> O,
70{
71    MapOk { iter, f: MapOkFn(f) }
72}
73
74#[derive(Clone)]
75#[must_use = "iterators are lazy and do nothing unless consumed"]
76pub struct MapOkBorrowBy<I, F> {
77    iter: I,
78    f: F,
79}
80
81pub trait MapOkBorrowPredicate<T> {
82    type Out;
83    fn call(&mut self, t: &T) -> Self::Out;
84}
85
86impl<I, F, T, E> Iterator for MapOkBorrowBy<I, F>
87where
88    I: Iterator<Item = Result<T, E>>,
89    F: MapOkBorrowPredicate<T>,
90{
91    type Item = Result<F::Out, E>;
92
93    fn next(&mut self) -> Option<Self::Item> {
94        self.iter.next().map(|e| e.map(|t| self.f.call(&t)))
95    }
96
97    fn size_hint(&self) -> (usize, Option<usize>) {
98        self.iter.size_hint()
99    }
100
101    fn fold<Acc, Fold>(self, init: Acc, mut fold_f: Fold) -> Acc
102    where
103        Self: Sized,
104        Fold: FnMut(Acc, Self::Item) -> Acc,
105    {
106        let mut f = self.f;
107        self.iter.fold(init, move |acc, v| fold_f(acc, v.map(|t| f.call(&t))))
108    }
109
110    fn collect<B: FromIterator<Self::Item>>(self) -> B
111    where
112        Self: Sized,
113    {
114        let mut f = self.f;
115        self.iter.map(move |v| v.map(|t| f.call(&t))).collect()
116    }
117}
118
119/// An iterator adaptor that maps the inner value
120///
121/// See [`.map_ok_borrow()`](crate::Iterflow::map_ok_borrow) for more information.
122pub type MapOkBorrow<I, F> = MapOkBorrowBy<I, MapOkBorrowFn<F>>;
123
124impl<F, T, O> MapOkBorrowPredicate<T> for MapOkBorrowFn<F>
125where
126    F: FnMut(&T) -> O,
127{
128    type Out = O;
129
130    fn call(&mut self, t: &T) -> Self::Out {
131        self.0(t)
132    }
133}
134
135#[derive(Clone)]
136pub struct MapOkBorrowFn<F>(F);
137
138/// Create a new `MapOkBorrow`.
139pub fn map_ok_borrow<I, F, E, O>(iter: I, f: F) -> MapOkBorrow<I, F>
140where
141    I: Iterator,
142    F: FnMut(&E) -> O,
143{
144    MapOkBorrow {
145        iter,
146        f: MapOkBorrowFn(f),
147    }
148}