Fix #119551: Rewrite Iterator::position default impl, storing the accumulating value outside of the fold in an attempt to improve code generation

Squashed with: Add inheriting overflow checks back
This commit is contained in:
martha 2024-01-05 03:34:21 +01:00 committed by marthadev
parent f688dd684f
commit 0e9c3d9533

View File

@ -3094,16 +3094,23 @@ pub trait Iterator {
P: FnMut(Self::Item) -> bool, P: FnMut(Self::Item) -> bool,
{ {
#[inline] #[inline]
fn check<T>( fn check<'a, T>(
mut predicate: impl FnMut(T) -> bool, mut predicate: impl FnMut(T) -> bool + 'a,
) -> impl FnMut(usize, T) -> ControlFlow<usize, usize> { acc: &'a mut usize,
) -> impl FnMut((), T) -> ControlFlow<usize, ()> + 'a {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
move |i, x| { move |_, x| {
if predicate(x) { ControlFlow::Break(i) } else { ControlFlow::Continue(i + 1) } if predicate(x) {
ControlFlow::Break(*acc)
} else {
*acc += 1;
ControlFlow::Continue(())
}
} }
} }
self.try_fold(0, check(predicate)).break_value() let mut acc = 0;
self.try_fold((), check(predicate, &mut acc)).break_value()
} }
/// Searches for an element in an iterator from the right, returning its /// Searches for an element in an iterator from the right, returning its