reduce indirection in for_each specialization

This commit is contained in:
The 8472 2023-08-28 14:35:51 +02:00
parent 72b01d5cca
commit 07a1d5f027

View File

@ -303,13 +303,12 @@ fn spec_fold<B, F>(mut self, init: B, mut f: F) -> B
}
#[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));
}
}