1#[derive(Clone)]
2#[must_use = "iterators are lazy and do nothing unless consumed"]
3pub struct AndThenBy<I, F> {
4 iter: I,
5 f: F,
6}
7
8pub trait AndThenPredicate<T> {
9 type Out;
10 type Err;
11 fn call(&mut self, t: T) -> Result<Self::Out, Self::Err>;
12}
13
14impl<I, F, T, E> Iterator for AndThenBy<I, F>
15where
16 I: Iterator<Item = Result<T, E>>,
17 F: AndThenPredicate<T>,
18 E: From<<F as AndThenPredicate<T>>::Err>,
19{
20 type Item = Result<F::Out, E>;
21
22 fn next(&mut self) -> Option<Self::Item> {
23 self.iter.next().map(|r| match r {
24 Ok(t) => match self.f.call(t) {
25 Ok(o) => Ok(o),
26 Err(err) => Err(err.into()),
27 },
28 Err(err) => Err(err),
29 })
30 }
31
32 fn size_hint(&self) -> (usize, Option<usize>) {
33 self.iter.size_hint()
34 }
35
36 fn fold<Acc, Fold>(self, init: Acc, mut fold_f: Fold) -> Acc
37 where
38 Self: Sized,
39 Fold: FnMut(Acc, Self::Item) -> Acc,
40 {
41 let mut f = self.f;
42 self.iter.fold(init, move |acc, r| {
43 fold_f(
44 acc,
45 match r {
46 Ok(t) => match f.call(t) {
47 Ok(o) => Ok(o),
48 Err(err) => Err(err.into()),
49 },
50 Err(err) => Err(err),
51 },
52 )
53 })
54 }
55
56 fn collect<B: FromIterator<Self::Item>>(self) -> B
57 where
58 Self: Sized,
59 {
60 let mut f = self.f;
61 self.iter
62 .map(move |r| match r {
63 Ok(t) => match f.call(t) {
64 Ok(o) => Ok(o),
65 Err(err) => Err(err.into()),
66 },
67 Err(err) => Err(err),
68 })
69 .collect()
70 }
71}
72
73pub type AndThen<I, F> = AndThenBy<I, AndThenFn<F>>;
77
78impl<F, T, O, E> AndThenPredicate<T> for AndThenFn<F>
79where
80 F: FnMut(T) -> Result<O, E>,
81{
82 type Err = E;
83 type Out = O;
84
85 fn call(&mut self, t: T) -> Result<Self::Out, Self::Err> {
86 self.0(t)
87 }
88}
89
90#[derive(Clone)]
91pub struct AndThenFn<F>(F);
92
93pub fn and_then<I, F, T, O, E>(iter: I, f: F) -> AndThen<I, F>
95where
96 I: Iterator,
97 F: FnMut(T) -> Result<O, E>,
98{
99 AndThen { iter, f: AndThenFn(f) }
100}
101
102#[derive(Clone)]
103#[must_use = "iterators are lazy and do nothing unless consumed"]
104pub struct AndThenBorrowBy<I, F> {
105 iter: I,
106 f: F,
107}
108
109pub trait AndThenBorrowPredicate<T> {
110 type Out;
111 type Err;
112 fn call(&mut self, t: &T) -> Result<Self::Out, Self::Err>;
113}
114
115impl<I, F, T, E> Iterator for AndThenBorrowBy<I, F>
116where
117 I: Iterator<Item = Result<T, E>>,
118 F: AndThenBorrowPredicate<T>,
119 E: From<<F as AndThenBorrowPredicate<T>>::Err>,
120{
121 type Item = Result<F::Out, E>;
122
123 fn next(&mut self) -> Option<Self::Item> {
124 self.iter.next().map(|r| match r {
125 Ok(t) => match self.f.call(&t) {
126 Ok(o) => Ok(o),
127 Err(err) => Err(err.into()),
128 },
129 Err(err) => Err(err),
130 })
131 }
132
133 fn size_hint(&self) -> (usize, Option<usize>) {
134 self.iter.size_hint()
135 }
136
137 fn fold<Acc, Fold>(self, init: Acc, mut fold_f: Fold) -> Acc
138 where
139 Self: Sized,
140 Fold: FnMut(Acc, Self::Item) -> Acc,
141 {
142 let mut f = self.f;
143 self.iter.fold(init, move |acc, r| {
144 fold_f(
145 acc,
146 match r {
147 Ok(t) => match f.call(&t) {
148 Ok(o) => Ok(o),
149 Err(err) => Err(err.into()),
150 },
151 Err(err) => Err(err),
152 },
153 )
154 })
155 }
156
157 fn collect<B: FromIterator<Self::Item>>(self) -> B
158 where
159 Self: Sized,
160 {
161 let mut f = self.f;
162 self.iter
163 .map(move |r| match r {
164 Ok(t) => match f.call(&t) {
165 Ok(o) => Ok(o),
166 Err(err) => Err(err.into()),
167 },
168 Err(err) => Err(err),
169 })
170 .collect()
171 }
172}
173
174pub type AndThenBorrow<I, F> = AndThenBorrowBy<I, AndThenBorrowFn<F>>;
178
179impl<F, T, O, E> AndThenBorrowPredicate<T> for AndThenBorrowFn<F>
180where
181 F: FnMut(&T) -> Result<O, E>,
182{
183 type Err = E;
184 type Out = O;
185
186 fn call(&mut self, t: &T) -> Result<Self::Out, Self::Err> {
187 self.0(t)
188 }
189}
190
191#[derive(Clone)]
192pub struct AndThenBorrowFn<F>(F);
193
194pub fn and_then_borrow<I, F, E, O>(iter: I, f: F) -> AndThenBorrow<I, F>
196where
197 I: Iterator,
198 F: FnMut(&E) -> O,
199{
200 AndThenBorrow {
201 iter,
202 f: AndThenBorrowFn(f),
203 }
204}