Skip to content

Commit b210413

Browse files
committed
Specialize the Scan::fold method in the same manner as Scan::try_fold.
(It is easier to subsequently optimize this body, rather than starting from `Scan::try_fold`.)
1 parent 6054658 commit b210413

File tree

1 file changed

+15
-0
lines changed
  • src/libcore/iter/adapters

1 file changed

+15
-0
lines changed

src/libcore/iter/adapters/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,21 @@ impl<B, I, St, F> Iterator for Scan<I, St, F> where
16721672
}
16731673
}).into_try()
16741674
}
1675+
1676+
#[inline]
1677+
fn fold<C, G>(mut self, init: C, mut g: G) -> C where
1678+
Self: Sized, G: FnMut(C, Self::Item) -> C,
1679+
{
1680+
// self.try_fold(init, move |acc, x| Ok::<C, !>(g(acc, x))).unwrap()
1681+
let state = &mut self.state;
1682+
let f = &mut self.f;
1683+
self.iter.fold(init, move |acc, x| {
1684+
match f(state, x) {
1685+
None => acc,
1686+
Some(x) => g(acc, x),
1687+
}
1688+
})
1689+
}
16751690
}
16761691

16771692
/// An iterator that yields `None` forever after the underlying iterator

0 commit comments

Comments
 (0)