From 21903532eee96a5311d08b8a9e9cc9f9231bc478 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Sun, 2 Aug 2020 10:35:57 -0700 Subject: [PATCH] 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. --- library/core/src/slice/mod.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 18ff50b8dbd..a32ea062de4 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1067,14 +1067,11 @@ impl [T] { pub fn array_chunks_mut(&mut self) -> ArrayChunksMut<'_, T, N> { assert_ne!(N, 0); let len = self.len() / N; - let (fst_ptr, snd) = { - // Scope the first slice into a pointer to avoid aliasing the new slice below. - let (fst, snd) = self.split_at_mut(len * N); - (fst.as_mut_ptr(), snd) - }; + let (fst, snd) = self.split_at_mut(len * N); // SAFETY: We cast a slice of `len * N` elements into // a slice of `len` many `N` elements chunks. - let array_slice: &mut [[T; N]] = unsafe { from_raw_parts_mut(fst_ptr.cast(), len) }; + let array_slice: &mut [[T; N]] = + unsafe { from_raw_parts_mut(fst.as_mut_ptr().cast(), len) }; ArrayChunksMut { iter: array_slice.iter_mut(), rem: snd } }