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 @@ impl Iterator for ArrayChunks #[inline] fn next(&mut self) -> Option { - 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 @@ impl DoubleEndedIterator for ArrayChunks { #[inline] fn next_back(&mut self) -> Option { - // 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(&mut self, init: B, mut f: F) -> R