From d991949fe1b4362fca5c3bee1309159cdfae73c0 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Tue, 20 Jul 2021 20:53:43 +0200 Subject: [PATCH] Specialize ProcessResults::fold --- src/process_results_impl.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/process_results_impl.rs b/src/process_results_impl.rs index 9da108b15..44308f378 100644 --- a/src/process_results_impl.rs +++ b/src/process_results_impl.rs @@ -30,6 +30,23 @@ impl<'a, I, T, E> Iterator for ProcessResults<'a, I, E> fn size_hint(&self) -> (usize, Option) { (0, self.iter.size_hint().1) } + + fn fold(mut self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + let error = self.error; + self.iter + .try_fold(init, |acc, opt| match opt { + Ok(x) => Ok(f(acc, x)), + Err(e) => { + *error = Err(e); + Err(acc) + } + }) + .unwrap_or_else(|e| e) + } } /// “Lift” a function of the values of an iterator so that it can process