| 100 | #[track_caller] |
| 101 | #[must_use] |
| 102 | const fn as_chunks_mut<T, const N: usize>(slice: &mut [T]) -> (&mut [[T; N]], &mut [T]) { |
| 103 | assert!(N != 0, "chunk size must be non-zero"); |
| 104 | let len_rounded_down = slice.len() / N * N; |
| 105 | // SAFETY: The rounded-down value is always the same or smaller than the |
| 106 | // original length, and thus must be in-bounds of the slice. |
| 107 | let (multiple_of_n, remainder) = unsafe { slice.split_at_mut_unchecked(len_rounded_down) }; |
| 108 | // SAFETY: We already panicked for zero, and ensured by construction |
| 109 | // that the length of the subslice is a multiple of N. Arrays have the same alignment as T. |
| 110 | let array_slice = unsafe { |
| 111 | slice::from_raw_parts_mut(multiple_of_n.as_mut_ptr().cast(), multiple_of_n.len() / N) |
| 112 | }; |
| 113 | (array_slice, remainder) |
| 114 | } |