| 1 | #[cfg (doc)] |
| 2 | use crate::Itertools; |
| 3 | |
| 4 | /// An iterator that produces only the `T` values as long as the |
| 5 | /// inner iterator produces `Ok(T)`. |
| 6 | /// |
| 7 | /// Used by [`process_results`](crate::process_results), see its docs |
| 8 | /// for more information. |
| 9 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed" ] |
| 10 | #[derive (Debug)] |
| 11 | pub struct ProcessResults<'a, I, E: 'a> { |
| 12 | error: &'a mut Result<(), E>, |
| 13 | iter: I, |
| 14 | } |
| 15 | |
| 16 | impl<'a, I, T, E> Iterator for ProcessResults<'a, I, E> |
| 17 | where |
| 18 | I: Iterator<Item = Result<T, E>>, |
| 19 | { |
| 20 | type Item = T; |
| 21 | |
| 22 | fn next(&mut self) -> Option<Self::Item> { |
| 23 | match self.iter.next() { |
| 24 | Some(Ok(x)) => Some(x), |
| 25 | Some(Err(e)) => { |
| 26 | *self.error = Err(e); |
| 27 | None |
| 28 | } |
| 29 | None => None, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 34 | (0, self.iter.size_hint().1) |
| 35 | } |
| 36 | |
| 37 | fn fold<B, F>(mut self, init: B, mut f: F) -> B |
| 38 | where |
| 39 | Self: Sized, |
| 40 | F: FnMut(B, Self::Item) -> B, |
| 41 | { |
| 42 | let error = self.error; |
| 43 | self.iter |
| 44 | .try_fold(init, |acc, opt| match opt { |
| 45 | Ok(x) => Ok(f(acc, x)), |
| 46 | Err(e) => { |
| 47 | *error = Err(e); |
| 48 | Err(acc) |
| 49 | } |
| 50 | }) |
| 51 | .unwrap_or_else(|e| e) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// “Lift” a function of the values of an iterator so that it can process |
| 56 | /// an iterator of `Result` values instead. |
| 57 | /// |
| 58 | /// [`IntoIterator`] enabled version of [`Itertools::process_results`]. |
| 59 | pub fn process_results<I, F, T, E, R>(iterable: I, processor: F) -> Result<R, E> |
| 60 | where |
| 61 | I: IntoIterator<Item = Result<T, E>>, |
| 62 | F: FnOnce(ProcessResults<I::IntoIter, E>) -> R, |
| 63 | { |
| 64 | let iter: ::IntoIter = iterable.into_iter(); |
| 65 | let mut error: Result<(), E> = Ok(()); |
| 66 | |
| 67 | let result: R = processor(ProcessResults { |
| 68 | error: &mut error, |
| 69 | iter, |
| 70 | }); |
| 71 | |
| 72 | error.map(|_| result) |
| 73 | } |
| 74 | |