Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Override try_fold for StepBy
  • Loading branch information
scottmcm committed Apr 29, 2018
commit 2d55d28a3cb249980cb30fffe191034e0a914326
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