Skip to content

Commit 2190353

Browse files
committed
Build the slice directly in array_chunks_mut
Review discussion found that the concern about aliasing was overblown, so we can simplify this to cast from one slice to another directly.
1 parent 864a28e commit 2190353

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

library/core/src/slice/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,14 +1067,11 @@ impl<T> [T] {
10671067
pub fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
10681068
assert_ne!(N, 0);
10691069
let len = self.len() / N;
1070-
let (fst_ptr, snd) = {
1071-
// Scope the first slice into a pointer to avoid aliasing the new slice below.
1072-
let (fst, snd) = self.split_at_mut(len * N);
1073-
(fst.as_mut_ptr(), snd)
1074-
};
1070+
let (fst, snd) = self.split_at_mut(len * N);
10751071
// SAFETY: We cast a slice of `len * N` elements into
10761072
// a slice of `len` many `N` elements chunks.
1077-
let array_slice: &mut [[T; N]] = unsafe { from_raw_parts_mut(fst_ptr.cast(), len) };
1073+
let array_slice: &mut [[T; N]] =
1074+
unsafe { from_raw_parts_mut(fst.as_mut_ptr().cast(), len) };
10781075
ArrayChunksMut { iter: array_slice.iter_mut(), rem: snd }
10791076
}
10801077

0 commit comments

Comments
 (0)