Skip to content

Commit

Permalink
reduce indirection in for_each specialization
Browse files Browse the repository at this point in the history
  • Loading branch information
the8472 committed Aug 28, 2023
1 parent 72b01d5 commit 07a1d5f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions library/core/src/iter/adapters/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,12 @@ impl<I: Iterator + TrustedRandomAccess> SpecTake for Take<I> {
}

#[inline]
fn spec_for_each<F: FnMut(Self::Item)>(self, f: F) {
// Based on the the Iterator trait default impl.
#[inline]
fn call<T>(mut f: impl FnMut(T)) -> impl FnMut((), T) {
move |(), item| f(item)
fn spec_for_each<F: FnMut(Self::Item)>(mut self, mut f: F) {
let end = self.n.min(self.iter.size());
for i in 0..end {
// SAFETY: i < end <= self.iter.size() and we discard the iterator at the end
let val = unsafe { self.iter.__iterator_get_unchecked(i) };
f(val);
}

self.spec_fold((), call(f));
}
}

0 comments on commit 07a1d5f

Please sign in to comment.