diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-08-01 18:26:18 +0400 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-08-01 18:26:18 +0400 |
| commit | b8b14864c08cb3d2830006377cbb7ac8741bbb91 (patch) | |
| tree | d0306510484250cf3acbdc60c9bc4a165ac180ba | |
| parent | ef72349e38635bc329a94b1e95648562e59ab7d2 (diff) | |
| download | rust-b8b14864c08cb3d2830006377cbb7ac8741bbb91.tar.gz rust-b8b14864c08cb3d2830006377cbb7ac8741bbb91.zip | |
Forward `ArrayChunks::next{,_back}` to `try_{for_each,rfold}`
(suggested in the review of the previous attempt to add `ArrayChunks`)
| -rw-r--r-- | library/core/src/iter/adapters/array_chunks.rs | 44 |
1 files changed, 2 insertions, 42 deletions
diff --git a/library/core/src/iter/adapters/array_chunks.rs b/library/core/src/iter/adapters/array_chunks.rs index 8f3e1b58b52..e25a6f9754b 100644 --- a/library/core/src/iter/adapters/array_chunks.rs +++ b/library/core/src/iter/adapters/array_chunks.rs @@ -48,33 +48,7 @@ where #[inline] fn next(&mut self) -> Option<Self::Item> { - let mut array = MaybeUninit::uninit_array(); - // SAFETY: `array` will still be valid if `guard` is dropped. - let mut guard = unsafe { FrontGuard::new(&mut array) }; - - for slot in array.iter_mut() { - match self.iter.next() { - Some(item) => { - slot.write(item); - guard.init += 1; - } - None => { - if guard.init > 0 { - let init = guard.init; - mem::forget(guard); - self.remainder = { - // SAFETY: `array` was initialized with `init` elements. - Some(unsafe { array::IntoIter::new_unchecked(array, 0..init) }) - }; - } - return None; - } - } - } - - mem::forget(guard); - // SAFETY: All elements of the array were populated in the loop above. - Some(unsafe { MaybeUninit::array_assume_init(array) }) + self.try_for_each(ControlFlow::Break).break_value() } #[inline] @@ -194,21 +168,7 @@ where { #[inline] fn next_back(&mut self) -> Option<Self::Item> { - // We are iterating from the back we need to first handle the remainder. - self.next_back_remainder()?; - - let mut array = MaybeUninit::uninit_array(); - // SAFETY: `array` will still be valid if `guard` is dropped. - let mut guard = unsafe { BackGuard::new(&mut array) }; - - for slot in array.iter_mut().rev() { - slot.write(self.iter.next_back()?); - guard.uninit -= 1; - } - - mem::forget(guard); - // SAFETY: All elements of the array were populated in the loop above. - Some(unsafe { MaybeUninit::array_assume_init(array) }) + self.try_rfold((), |(), x| ControlFlow::Break(x)).break_value() } fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R |
