From 2d55d28a3cb249980cb30fffe191034e0a914326 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 29 Apr 2018 02:12:06 -0700 Subject: [PATCH] Override try_fold for StepBy --- src/libcore/iter/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 1e8476d3880c8..955bbde5ff69f 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -754,6 +754,26 @@ impl Iterator for StepBy where I: Iterator { self.iter.nth(nth - 1); } } + + fn try_fold(&mut self, init: B, mut f: F) -> R where + Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try + { + let mut accum = init; + + if self.first_take { + self.first_take = false; + if let Some(x) = self.iter.next() { + accum = f(accum, x)?; + } else { + return Try::from_ok(accum); + } + } + + while let Some(x) = self.iter.nth(self.step) { + accum = f(accum, x)?; + } + Try::from_ok(accum) + } } // StepBy can only make the iterator shorter, so the len will still fit.