@@ -1418,18 +1418,17 @@ impl<'a, T> Iterator for Chunks<'a, T> {
1418
1418
#[ doc( hidden) ]
1419
1419
unsafe fn __iterator_get_unchecked ( & mut self , idx : usize ) -> Self :: Item {
1420
1420
let start = idx * self . chunk_size ;
1421
- let end = match start. checked_add ( self . chunk_size ) {
1422
- None => self . v . len ( ) ,
1423
- Some ( end) => cmp:: min ( end, self . v . len ( ) ) ,
1424
- } ;
1425
1421
// SAFETY: the caller guarantees that `i` is in bounds,
1426
1422
// which means that `start` must be in bounds of the
1427
- // underlying `self.v` slice, and we made sure that `end `
1423
+ // underlying `self.v` slice, and we made sure that `len `
1428
1424
// is also in bounds of `self.v`. Thus, `start` cannot overflow
1429
1425
// an `isize`, and the slice constructed by `from_raw_parts`
1430
1426
// is a subslice of `self.v` which is guaranteed to be valid
1431
1427
// for the lifetime `'a` of `self.v`.
1432
- unsafe { from_raw_parts ( self . v . as_ptr ( ) . add ( start) , end - start) }
1428
+ unsafe {
1429
+ let len = cmp:: min ( self . v . len ( ) . unchecked_sub ( start) , self . chunk_size ) ;
1430
+ from_raw_parts ( self . v . as_ptr ( ) . add ( start) , len)
1431
+ }
1433
1432
}
1434
1433
}
1435
1434
@@ -1457,7 +1456,7 @@ impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
1457
1456
} else {
1458
1457
let start = ( len - 1 - n) * self . chunk_size ;
1459
1458
let end = match start. checked_add ( self . chunk_size ) {
1460
- Some ( res) => cmp:: min ( res , self . v . len ( ) ) ,
1459
+ Some ( res) => cmp:: min ( self . v . len ( ) , res ) ,
1461
1460
None => self . v . len ( ) ,
1462
1461
} ;
1463
1462
let nth_back = & self . v [ start..end] ;
@@ -1579,17 +1578,16 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
1579
1578
#[ doc( hidden) ]
1580
1579
unsafe fn __iterator_get_unchecked ( & mut self , idx : usize ) -> Self :: Item {
1581
1580
let start = idx * self . chunk_size ;
1582
- let end = match start. checked_add ( self . chunk_size ) {
1583
- None => self . v . len ( ) ,
1584
- Some ( end) => cmp:: min ( end, self . v . len ( ) ) ,
1585
- } ;
1586
1581
// SAFETY: see comments for `Chunks::__iterator_get_unchecked`.
1587
1582
//
1588
1583
// Also note that the caller also guarantees that we're never called
1589
1584
// with the same index again, and that no other methods that will
1590
1585
// access this subslice are called, so it is valid for the returned
1591
1586
// slice to be mutable.
1592
- unsafe { from_raw_parts_mut ( self . v . as_mut_ptr ( ) . add ( start) , end - start) }
1587
+ unsafe {
1588
+ let len = cmp:: min ( self . v . len ( ) . unchecked_sub ( start) , self . chunk_size ) ;
1589
+ from_raw_parts_mut ( self . v . as_mut_ptr ( ) . add ( start) , len)
1590
+ }
1593
1591
}
1594
1592
}
1595
1593
@@ -1619,7 +1617,7 @@ impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
1619
1617
} else {
1620
1618
let start = ( len - 1 - n) * self . chunk_size ;
1621
1619
let end = match start. checked_add ( self . chunk_size ) {
1622
- Some ( res) => cmp:: min ( res , self . v . len ( ) ) ,
1620
+ Some ( res) => cmp:: min ( self . v . len ( ) , res ) ,
1623
1621
None => self . v . len ( ) ,
1624
1622
} ;
1625
1623
let ( temp, _tail) = mem:: replace ( & mut self . v , & mut [ ] ) . split_at_mut ( end) ;
0 commit comments