Skip to content

Commit b8b1486

Browse files
committed
Forward ArrayChunks::next{,_back} to try_{for_each,rfold}
(suggested in the review of the previous attempt to add `ArrayChunks`)
1 parent ef72349 commit b8b1486

File tree

1 file changed

+2
-42
lines changed

1 file changed

+2
-42
lines changed

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,7 @@ where
4848

4949
#[inline]
5050
fn next(&mut self) -> Option<Self::Item> {
51-
let mut array = MaybeUninit::uninit_array();
52-
// SAFETY: `array` will still be valid if `guard` is dropped.
53-
let mut guard = unsafe { FrontGuard::new(&mut array) };
54-
55-
for slot in array.iter_mut() {
56-
match self.iter.next() {
57-
Some(item) => {
58-
slot.write(item);
59-
guard.init += 1;
60-
}
61-
None => {
62-
if guard.init > 0 {
63-
let init = guard.init;
64-
mem::forget(guard);
65-
self.remainder = {
66-
// SAFETY: `array` was initialized with `init` elements.
67-
Some(unsafe { array::IntoIter::new_unchecked(array, 0..init) })
68-
};
69-
}
70-
return None;
71-
}
72-
}
73-
}
74-
75-
mem::forget(guard);
76-
// SAFETY: All elements of the array were populated in the loop above.
77-
Some(unsafe { MaybeUninit::array_assume_init(array) })
51+
self.try_for_each(ControlFlow::Break).break_value()
7852
}
7953

8054
#[inline]
@@ -194,21 +168,7 @@ where
194168
{
195169
#[inline]
196170
fn next_back(&mut self) -> Option<Self::Item> {
197-
// We are iterating from the back we need to first handle the remainder.
198-
self.next_back_remainder()?;
199-
200-
let mut array = MaybeUninit::uninit_array();
201-
// SAFETY: `array` will still be valid if `guard` is dropped.
202-
let mut guard = unsafe { BackGuard::new(&mut array) };
203-
204-
for slot in array.iter_mut().rev() {
205-
slot.write(self.iter.next_back()?);
206-
guard.uninit -= 1;
207-
}
208-
209-
mem::forget(guard);
210-
// SAFETY: All elements of the array were populated in the loop above.
211-
Some(unsafe { MaybeUninit::array_assume_init(array) })
171+
self.try_rfold((), |(), x| ControlFlow::Break(x)).break_value()
212172
}
213173

214174
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R

0 commit comments

Comments
 (0)