Skip to content

Commit be90cf3

Browse files
committed
undeprecate and optimize fold_while
Prompted by #469.
1 parent c48da7b commit be90cf3

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/lib.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -2099,19 +2099,26 @@ pub trait Itertools : Iterator {
20992099
/// The big difference between the computations of `result2` and `result3` is that while
21002100
/// `fold()` called the provided closure for every item of the callee iterator,
21012101
/// `fold_while()` actually stopped iterating as soon as it encountered `Fold::Done(_)`.
2102-
#[deprecated(note="Use .try_fold() instead", since="0.8.0")]
21032102
fn fold_while<B, F>(&mut self, init: B, mut f: F) -> FoldWhile<B>
21042103
where Self: Sized,
21052104
F: FnMut(B, Self::Item) -> FoldWhile<B>
21062105
{
2107-
let mut acc = init;
2108-
for item in self {
2109-
match f(acc, item) {
2110-
FoldWhile::Continue(res) => acc = res,
2111-
res @ FoldWhile::Done(_) => return res,
2106+
use Result::{
2107+
Ok as Continue,
2108+
Err as Break,
2109+
};
2110+
2111+
let result = self.try_fold(init, #[inline(always)] |acc, v|
2112+
match f(acc, v) {
2113+
FoldWhile::Continue(acc) => Continue(acc),
2114+
FoldWhile::Done(acc) => Break(acc),
21122115
}
2116+
);
2117+
2118+
match result {
2119+
Continue(acc) => FoldWhile::Continue(acc),
2120+
Break(acc) => FoldWhile::Done(acc),
21132121
}
2114-
FoldWhile::Continue(acc)
21152122
}
21162123

21172124
/// Iterate over the entire iterator and add all the elements.

0 commit comments

Comments
 (0)