2 /// An iterator that produces only the `T` values as long as the
3 /// inner iterator produces `Ok(T)`.
5 /// Used by [`process_results`](../fn.process_results.html), see its docs
6 /// for more information.
7 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
9 pub struct ProcessResults
<'a
, I
, E
: 'a
> {
10 error
: &'a
mut Result
<(), E
>,
14 impl<'a
, I
, T
, E
> Iterator
for ProcessResults
<'a
, I
, E
>
15 where I
: Iterator
<Item
= Result
<T
, E
>>
19 fn next(&mut self) -> Option
<Self::Item
> {
20 match self.iter
.next() {
21 Some(Ok(x
)) => Some(x
),
30 fn size_hint(&self) -> (usize, Option
<usize>) {
31 let (_
, hi
) = self.iter
.size_hint();
36 /// “Lift” a function of the values of an iterator so that it can process
37 /// an iterator of `Result` values instead.
39 /// `iterable` is an iterator or iterable with `Result<T, E>` elements, where
40 /// `T` is the value type and `E` the error type.
42 /// `processor` is a closure that receives an adapted version of the iterable
43 /// as the only argument — the adapted iterator produces elements of type `T`,
44 /// as long as the original iterator produces `Ok` values.
46 /// If the original iterable produces an error at any point, the adapted
47 /// iterator ends and the `process_results` function will return the
50 /// Otherwise, the return value from the closure is returned wrapped
56 /// use itertools::process_results;
58 /// type R = Result<i32, &'static str>;
60 /// let first_values: Vec<R> = vec![Ok(1), Ok(0), Ok(3)];
61 /// let second_values: Vec<R> = vec![Ok(2), Ok(1), Err("overflow")];
63 /// // “Lift” the iterator .max() method to work on the values in Results using process_results
65 /// let first_max = process_results(first_values, |iter| iter.max().unwrap_or(0));
66 /// let second_max = process_results(second_values, |iter| iter.max().unwrap_or(0));
68 /// assert_eq!(first_max, Ok(3));
69 /// assert!(second_max.is_err());
71 pub fn process_results
<I
, F
, T
, E
, R
>(iterable
: I
, processor
: F
) -> Result
<R
, E
>
72 where I
: IntoIterator
<Item
= Result
<T
, E
>>,
73 F
: FnOnce(ProcessResults
<I
::IntoIter
, E
>) -> R
75 let iter
= iterable
.into_iter();
76 let mut error
= Ok(());
78 let result
= processor(ProcessResults { error: &mut error, iter: iter }
);