]> git.proxmox.com Git - rustc.git/blame - vendor/itertools-0.8.2/src/process_results_impl.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / vendor / itertools-0.8.2 / src / process_results_impl.rs
CommitLineData
532ac7d7
XL
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)]
9pub struct ProcessResults<'a, I, E: 'a> {
10 error: &'a mut Result<(), E>,
11 iter: I,
12}
13
14impl<'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 let (_, hi) = self.iter.size_hint();
32 (0, hi)
33 }
34}
35
36/// “Lift” a function of the values of an iterator so that it can process
37/// an iterator of `Result` values instead.
38///
39/// `iterable` is an iterator or iterable with `Result<T, E>` elements, where
40/// `T` is the value type and `E` the error type.
41///
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.
45///
46/// If the original iterable produces an error at any point, the adapted
47/// iterator ends and the `process_results` function will return the
48/// error iself.
49///
50/// Otherwise, the return value from the closure is returned wrapped
51/// inside `Ok`.
52///
53/// # Example
54///
55/// ```
56/// use itertools::process_results;
57///
58/// type R = Result<i32, &'static str>;
59///
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")];
62///
63/// // “Lift” the iterator .max() method to work on the values in Results using process_results
64///
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));
67///
68/// assert_eq!(first_max, Ok(3));
69/// assert!(second_max.is_err());
70/// ```
71pub 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
74{
75 let iter = iterable.into_iter();
76 let mut error = Ok(());
77
78 let result = processor(ProcessResults { error: &mut error, iter: iter });
79
80 error.map(|_| result)
81}