Skip to content

Commit

Permalink
Override try_fold for StepBy
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Apr 29, 2018
1 parent 66363b2 commit 2d55d28
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,26 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
self.iter.nth(nth - 1);
}
}

fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
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.
Expand Down

0 comments on commit 2d55d28

Please sign in to comment.