itertools/
process_results_impl.rs

1#[cfg(doc)]
2use 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)]
11pub struct ProcessResults<'a, I, E: 'a> {
12    error: &'a mut Result<(), E>,
13    iter: I,
14}
15
16impl<'a, I, T, E> Iterator for ProcessResults<'a, I, E>
17    where I: Iterator<Item = Result<T, E>>
18{
19    type Item = T;
20
21    fn next(&mut self) -> Option<Self::Item> {
22        match self.iter.next() {
23            Some(Ok(x)) => Some(x),
24            Some(Err(e)) => {
25                *self.error = Err(e);
26                None
27            }
28            None => None,
29        }
30    }
31
32    fn size_hint(&self) -> (usize, Option<usize>) {
33        (0, self.iter.size_hint().1)
34    }
35
36    fn fold<B, F>(mut self, init: B, mut f: F) -> B
37    where
38        Self: Sized,
39        F: FnMut(B, Self::Item) -> B,
40    {
41        let error = self.error;
42        self.iter
43            .try_fold(init, |acc, opt| match opt {
44                Ok(x) => Ok(f(acc, x)),
45                Err(e) => {
46                    *error = Err(e);
47                    Err(acc)
48                }
49            })
50            .unwrap_or_else(|e| e)
51    }
52}
53
54/// “Lift” a function of the values of an iterator so that it can process
55/// an iterator of `Result` values instead.
56///
57/// [`IntoIterator`] enabled version of [`Itertools::process_results`].
58pub fn process_results<I, F, T, E, R>(iterable: I, processor: F) -> Result<R, E>
59    where I: IntoIterator<Item = Result<T, E>>,
60          F: FnOnce(ProcessResults<I::IntoIter, E>) -> R
61{
62    let iter = iterable.into_iter();
63    let mut error = Ok(());
64
65    let result = processor(ProcessResults { error: &mut error, iter });
66
67    error.map(|_| result)
68}