Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 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`](../fn.process_results.html), 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>) { |
Joel Galenson | 6f79871 | 2021-04-01 17:03:06 -0700 | [diff] [blame] | 31 | (0, self.iter.size_hint().1) |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | /// “Lift” a function of the values of an iterator so that it can process |
| 36 | /// an iterator of `Result` values instead. |
| 37 | /// |
| 38 | /// `iterable` is an iterator or iterable with `Result<T, E>` elements, where |
| 39 | /// `T` is the value type and `E` the error type. |
| 40 | /// |
| 41 | /// `processor` is a closure that receives an adapted version of the iterable |
| 42 | /// as the only argument — the adapted iterator produces elements of type `T`, |
| 43 | /// as long as the original iterator produces `Ok` values. |
| 44 | /// |
| 45 | /// If the original iterable produces an error at any point, the adapted |
| 46 | /// iterator ends and the `process_results` function will return the |
| 47 | /// error iself. |
| 48 | /// |
| 49 | /// Otherwise, the return value from the closure is returned wrapped |
| 50 | /// inside `Ok`. |
| 51 | /// |
| 52 | /// # Example |
| 53 | /// |
| 54 | /// ``` |
| 55 | /// use itertools::process_results; |
| 56 | /// |
| 57 | /// type R = Result<i32, &'static str>; |
| 58 | /// |
| 59 | /// let first_values: Vec<R> = vec![Ok(1), Ok(0), Ok(3)]; |
| 60 | /// let second_values: Vec<R> = vec![Ok(2), Ok(1), Err("overflow")]; |
| 61 | /// |
| 62 | /// // “Lift” the iterator .max() method to work on the values in Results using process_results |
| 63 | /// |
| 64 | /// let first_max = process_results(first_values, |iter| iter.max().unwrap_or(0)); |
| 65 | /// let second_max = process_results(second_values, |iter| iter.max().unwrap_or(0)); |
| 66 | /// |
| 67 | /// assert_eq!(first_max, Ok(3)); |
| 68 | /// assert!(second_max.is_err()); |
| 69 | /// ``` |
| 70 | pub fn process_results<I, F, T, E, R>(iterable: I, processor: F) -> Result<R, E> |
| 71 | where I: IntoIterator<Item = Result<T, E>>, |
| 72 | F: FnOnce(ProcessResults<I::IntoIter, E>) -> R |
| 73 | { |
| 74 | let iter = iterable.into_iter(); |
| 75 | let mut error = Ok(()); |
| 76 | |
| 77 | let result = processor(ProcessResults { error: &mut error, iter }); |
| 78 | |
| 79 | error.map(|_| result) |
| 80 | } |