|
1 | 1 | use crate::ops::{Mul, Add};
|
2 | 2 | use crate::num::Wrapping;
|
| 3 | +use crate::iter::adapters::{OptionShunt, ResultShunt}; |
3 | 4 |
|
4 | 5 | /// Trait to represent types that can be created by summing up an iterator.
|
5 | 6 | ///
|
@@ -114,74 +115,6 @@ macro_rules! float_sum_product {
|
114 | 115 | integer_sum_product! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
|
115 | 116 | float_sum_product! { f32 f64 }
|
116 | 117 |
|
117 |
| -/// An iterator adapter that produces output as long as the underlying |
118 |
| -/// iterator produces `Result::Ok` values. |
119 |
| -/// |
120 |
| -/// If an error is encountered, the iterator stops and the error is |
121 |
| -/// stored. The error may be recovered later via `reconstruct`. |
122 |
| -struct ResultShunt<I, E> { |
123 |
| - iter: I, |
124 |
| - error: Option<E>, |
125 |
| -} |
126 |
| - |
127 |
| -impl<I, T, E> ResultShunt<I, E> |
128 |
| - where I: Iterator<Item = Result<T, E>> |
129 |
| -{ |
130 |
| - /// Process the given iterator as if it yielded a `T` instead of a |
131 |
| - /// `Result<T, _>`. Any errors will stop the inner iterator and |
132 |
| - /// the overall result will be an error. |
133 |
| - pub fn process<F, U>(iter: I, mut f: F) -> Result<U, E> |
134 |
| - where F: FnMut(&mut Self) -> U |
135 |
| - { |
136 |
| - let mut shunt = ResultShunt::new(iter); |
137 |
| - let value = f(shunt.by_ref()); |
138 |
| - shunt.reconstruct(value) |
139 |
| - } |
140 |
| - |
141 |
| - fn new(iter: I) -> Self { |
142 |
| - ResultShunt { |
143 |
| - iter, |
144 |
| - error: None, |
145 |
| - } |
146 |
| - } |
147 |
| - |
148 |
| - /// Consume the adapter and rebuild a `Result` value. This should |
149 |
| - /// *always* be called, otherwise any potential error would be |
150 |
| - /// lost. |
151 |
| - fn reconstruct<U>(self, val: U) -> Result<U, E> { |
152 |
| - match self.error { |
153 |
| - None => Ok(val), |
154 |
| - Some(e) => Err(e), |
155 |
| - } |
156 |
| - } |
157 |
| -} |
158 |
| - |
159 |
| -impl<I, T, E> Iterator for ResultShunt<I, E> |
160 |
| - where I: Iterator<Item = Result<T, E>> |
161 |
| -{ |
162 |
| - type Item = T; |
163 |
| - |
164 |
| - fn next(&mut self) -> Option<Self::Item> { |
165 |
| - match self.iter.next() { |
166 |
| - Some(Ok(v)) => Some(v), |
167 |
| - Some(Err(e)) => { |
168 |
| - self.error = Some(e); |
169 |
| - None |
170 |
| - } |
171 |
| - None => None, |
172 |
| - } |
173 |
| - } |
174 |
| - |
175 |
| - fn size_hint(&self) -> (usize, Option<usize>) { |
176 |
| - if self.error.is_some() { |
177 |
| - (0, Some(0)) |
178 |
| - } else { |
179 |
| - let (_, upper) = self.iter.size_hint(); |
180 |
| - (0, upper) |
181 |
| - } |
182 |
| - } |
183 |
| -} |
184 |
| - |
185 | 118 | #[stable(feature = "iter_arith_traits_result", since="1.16.0")]
|
186 | 119 | impl<T, U, E> Sum<Result<U, E>> for Result<T, E>
|
187 | 120 | where T: Sum<U>,
|
@@ -224,73 +157,6 @@ impl<T, U, E> Product<Result<U, E>> for Result<T, E>
|
224 | 157 | }
|
225 | 158 | }
|
226 | 159 |
|
227 |
| -/// An iterator adapter that produces output as long as the underlying |
228 |
| -/// iterator produces `Option::Some` values. |
229 |
| -struct OptionShunt<I> { |
230 |
| - iter: I, |
231 |
| - exited_early: bool, |
232 |
| -} |
233 |
| - |
234 |
| -impl<I, T> OptionShunt<I> |
235 |
| -where |
236 |
| - I: Iterator<Item = Option<T>>, |
237 |
| -{ |
238 |
| - /// Process the given iterator as if it yielded a `T` instead of a |
239 |
| - /// `Option<T>`. Any `None` value will stop the inner iterator and |
240 |
| - /// the overall result will be a `None`. |
241 |
| - pub fn process<F, U>(iter: I, mut f: F) -> Option<U> |
242 |
| - where |
243 |
| - F: FnMut(&mut Self) -> U, |
244 |
| - { |
245 |
| - let mut shunt = OptionShunt::new(iter); |
246 |
| - let value = f(shunt.by_ref()); |
247 |
| - shunt.reconstruct(value) |
248 |
| - } |
249 |
| - |
250 |
| - fn new(iter: I) -> Self { |
251 |
| - OptionShunt { |
252 |
| - iter, |
253 |
| - exited_early: false, |
254 |
| - } |
255 |
| - } |
256 |
| - |
257 |
| - /// Consume the adapter and rebuild a `Option` value. |
258 |
| - fn reconstruct<U>(self, val: U) -> Option<U> { |
259 |
| - if self.exited_early { |
260 |
| - None |
261 |
| - } else { |
262 |
| - Some(val) |
263 |
| - } |
264 |
| - } |
265 |
| -} |
266 |
| - |
267 |
| -impl<I, T> Iterator for OptionShunt<I> |
268 |
| -where |
269 |
| - I: Iterator<Item = Option<T>>, |
270 |
| -{ |
271 |
| - type Item = T; |
272 |
| - |
273 |
| - fn next(&mut self) -> Option<Self::Item> { |
274 |
| - match self.iter.next() { |
275 |
| - Some(Some(v)) => Some(v), |
276 |
| - Some(None) => { |
277 |
| - self.exited_early = true; |
278 |
| - None |
279 |
| - } |
280 |
| - None => None, |
281 |
| - } |
282 |
| - } |
283 |
| - |
284 |
| - fn size_hint(&self) -> (usize, Option<usize>) { |
285 |
| - if self.exited_early { |
286 |
| - (0, Some(0)) |
287 |
| - } else { |
288 |
| - let (_, upper) = self.iter.size_hint(); |
289 |
| - (0, upper) |
290 |
| - } |
291 |
| - } |
292 |
| -} |
293 |
| - |
294 | 160 | #[stable(feature = "iter_arith_traits_option", since = "1.37.0")]
|
295 | 161 | impl<T, U> Sum<Option<U>> for Option<T>
|
296 | 162 | where
|
|
0 commit comments