diff options
| author | The 8472 <git@infinite-source.de> | 2023-08-28 14:35:51 +0200 |
|---|---|---|
| committer | The 8472 <git@infinite-source.de> | 2023-08-28 14:37:31 +0200 |
| commit | 07a1d5f0273fdcce5ec7ec3cba9a6db232d9124b (patch) | |
| tree | 9f626e00cfbb3af31914db04b55dd30c683567f7 | |
| parent | 72b01d5cca0604e1ea5818d90b9feefae92a0093 (diff) | |
| download | rust-07a1d5f0273fdcce5ec7ec3cba9a6db232d9124b.tar.gz rust-07a1d5f0273fdcce5ec7ec3cba9a6db232d9124b.zip | |
reduce indirection in for_each specialization
| -rw-r--r-- | library/core/src/iter/adapters/take.rs | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/library/core/src/iter/adapters/take.rs b/library/core/src/iter/adapters/take.rs index 70252e075b9..c1d8cc4ff57 100644 --- a/library/core/src/iter/adapters/take.rs +++ b/library/core/src/iter/adapters/take.rs @@ -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)); } } |
