@@ -1337,8 +1337,10 @@ impl<T> [T] {
1337
1337
#[ must_use]
1338
1338
pub const fn as_chunks < const N : usize > ( & self ) -> ( & [ [ T ; N ] ] , & [ T ] ) {
1339
1339
assert ! ( N != 0 , "chunk size must be non-zero" ) ;
1340
- let len = self . len ( ) / N ;
1341
- let ( multiple_of_n, remainder) = self . split_at ( len * N ) ;
1340
+ let len_rounded_down = self . len ( ) / N * N ;
1341
+ // SAFETY: The rounded-down value is always the same or smaller than the
1342
+ // original length, and thus must be in-bounds of the slice.
1343
+ let ( multiple_of_n, remainder) = unsafe { self . split_at_unchecked ( len_rounded_down) } ;
1342
1344
// SAFETY: We already panicked for zero, and ensured by construction
1343
1345
// that the length of the subslice is a multiple of N.
1344
1346
let array_slice = unsafe { multiple_of_n. as_chunks_unchecked ( ) } ;
@@ -1487,8 +1489,10 @@ impl<T> [T] {
1487
1489
#[ must_use]
1488
1490
pub const fn as_chunks_mut < const N : usize > ( & mut self ) -> ( & mut [ [ T ; N ] ] , & mut [ T ] ) {
1489
1491
assert ! ( N != 0 , "chunk size must be non-zero" ) ;
1490
- let len = self . len ( ) / N ;
1491
- let ( multiple_of_n, remainder) = self . split_at_mut ( len * N ) ;
1492
+ let len_rounded_down = self . len ( ) / N * N ;
1493
+ // SAFETY: The rounded-down value is always the same or smaller than the
1494
+ // original length, and thus must be in-bounds of the slice.
1495
+ let ( multiple_of_n, remainder) = unsafe { self . split_at_mut_unchecked ( len_rounded_down) } ;
1492
1496
// SAFETY: We already panicked for zero, and ensured by construction
1493
1497
// that the length of the subslice is a multiple of N.
1494
1498
let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_mut ( ) } ;
0 commit comments