Skip to content

Commit 07a1d5f

Browse files
committed
reduce indirection in for_each specialization
1 parent 72b01d5 commit 07a1d5f

File tree

1 file changed

+6
-7
lines changed
  • library/core/src/iter/adapters

1 file changed

+6
-7
lines changed

library/core/src/iter/adapters/take.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,12 @@ impl<I: Iterator + TrustedRandomAccess> SpecTake for Take<I> {
303303
}
304304

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

0 commit comments

Comments
 (0)