1 | |
2 | /// An iterator that produces only the `T` values as long as the |
3 | /// inner iterator produces `Ok(T)`. |
4 | /// |
5 | /// Used by [`process_results`](crate::process_results), see its docs |
6 | /// for more information. |
7 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed" ] |
8 | #[derive(Debug)] |
9 | pub struct ProcessResults<'a, I, E: 'a> { |
10 | error: &'a mut Result<(), E>, |
11 | iter: I, |
12 | } |
13 | |
14 | impl<'a, I, T, E> Iterator for ProcessResults<'a, I, E> |
15 | where I: Iterator<Item = Result<T, E>> |
16 | { |
17 | type Item = T; |
18 | |
19 | fn next(&mut self) -> Option<Self::Item> { |
20 | match self.iter.next() { |
21 | Some(Ok(x)) => Some(x), |
22 | Some(Err(e)) => { |
23 | *self.error = Err(e); |
24 | None |
25 | } |
26 | None => None, |
27 | } |
28 | } |
29 | |
30 | fn size_hint(&self) -> (usize, Option<usize>) { |
31 | (0, self.iter.size_hint().1) |
32 | } |
33 | |
34 | fn fold<B, F>(mut self, init: B, mut f: F) -> B |
35 | where |
36 | Self: Sized, |
37 | F: FnMut(B, Self::Item) -> B, |
38 | { |
39 | let error = self.error; |
40 | self.iter |
41 | .try_fold(init, |acc, opt| match opt { |
42 | Ok(x) => Ok(f(acc, x)), |
43 | Err(e) => { |
44 | *error = Err(e); |
45 | Err(acc) |
46 | } |
47 | }) |
48 | .unwrap_or_else(|e| e) |
49 | } |
50 | } |
51 | |
52 | /// “Lift” a function of the values of an iterator so that it can process |
53 | /// an iterator of `Result` values instead. |
54 | /// |
55 | /// `iterable` is an iterator or iterable with `Result<T, E>` elements, where |
56 | /// `T` is the value type and `E` the error type. |
57 | /// |
58 | /// `processor` is a closure that receives an adapted version of the iterable |
59 | /// as the only argument — the adapted iterator produces elements of type `T`, |
60 | /// as long as the original iterator produces `Ok` values. |
61 | /// |
62 | /// If the original iterable produces an error at any point, the adapted |
63 | /// iterator ends and the `process_results` function will return the |
64 | /// error iself. |
65 | /// |
66 | /// Otherwise, the return value from the closure is returned wrapped |
67 | /// inside `Ok`. |
68 | /// |
69 | /// # Example |
70 | /// |
71 | /// ``` |
72 | /// use itertools::process_results; |
73 | /// |
74 | /// type R = Result<i32, &'static str>; |
75 | /// |
76 | /// let first_values: Vec<R> = vec![Ok(1), Ok(0), Ok(3)]; |
77 | /// let second_values: Vec<R> = vec![Ok(2), Ok(1), Err("overflow" )]; |
78 | /// |
79 | /// // “Lift” the iterator .max() method to work on the values in Results using process_results |
80 | /// |
81 | /// let first_max = process_results(first_values, |iter| iter.max().unwrap_or(0)); |
82 | /// let second_max = process_results(second_values, |iter| iter.max().unwrap_or(0)); |
83 | /// |
84 | /// assert_eq!(first_max, Ok(3)); |
85 | /// assert!(second_max.is_err()); |
86 | /// ``` |
87 | pub fn process_results<I, F, T, E, R>(iterable: I, processor: F) -> Result<R, E> |
88 | where I: IntoIterator<Item = Result<T, E>>, |
89 | F: FnOnce(ProcessResults<I::IntoIter, E>) -> R |
90 | { |
91 | let iter = iterable.into_iter(); |
92 | let mut error = Ok(()); |
93 | |
94 | let result = processor(ProcessResults { error: &mut error, iter }); |
95 | |
96 | error.map(|_| result) |
97 | } |
98 | |