mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
Implement as_chunks with split_at_unchecked
This commit is contained in:
@@ -1337,8 +1337,10 @@ pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
|
||||
#[must_use]
|
||||
pub const fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
|
||||
assert!(N != 0, "chunk size must be non-zero");
|
||||
let len = self.len() / N;
|
||||
let (multiple_of_n, remainder) = self.split_at(len * N);
|
||||
let len_rounded_down = self.len() / N * N;
|
||||
// SAFETY: The rounded-down value is always the same or smaller than the
|
||||
// original length, and thus must be in-bounds of the slice.
|
||||
let (multiple_of_n, remainder) = unsafe { self.split_at_unchecked(len_rounded_down) };
|
||||
// SAFETY: We already panicked for zero, and ensured by construction
|
||||
// that the length of the subslice is a multiple of N.
|
||||
let array_slice = unsafe { multiple_of_n.as_chunks_unchecked() };
|
||||
@@ -1487,8 +1489,10 @@ pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
|
||||
#[must_use]
|
||||
pub const fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
|
||||
assert!(N != 0, "chunk size must be non-zero");
|
||||
let len = self.len() / N;
|
||||
let (multiple_of_n, remainder) = self.split_at_mut(len * N);
|
||||
let len_rounded_down = self.len() / N * N;
|
||||
// SAFETY: The rounded-down value is always the same or smaller than the
|
||||
// original length, and thus must be in-bounds of the slice.
|
||||
let (multiple_of_n, remainder) = unsafe { self.split_at_mut_unchecked(len_rounded_down) };
|
||||
// SAFETY: We already panicked for zero, and ensured by construction
|
||||
// that the length of the subslice is a multiple of N.
|
||||
let array_slice = unsafe { multiple_of_n.as_chunks_unchecked_mut() };
|
||||
|
||||
Reference in New Issue
Block a user