@@ -1418,18 +1418,17 @@ impl<'a, T> Iterator for Chunks<'a, T> {
14181418 #[ doc( hidden) ]
14191419 unsafe fn __iterator_get_unchecked ( & mut self , idx : usize ) -> Self :: Item {
14201420 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- } ;
14251421 // SAFETY: the caller guarantees that `i` is in bounds,
14261422 // 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 `
14281424 // is also in bounds of `self.v`. Thus, `start` cannot overflow
14291425 // an `isize`, and the slice constructed by `from_raw_parts`
14301426 // is a subslice of `self.v` which is guaranteed to be valid
14311427 // 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+ }
14331432 }
14341433}
14351434
@@ -1457,7 +1456,7 @@ impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
14571456 } else {
14581457 let start = ( len - 1 - n) * self . chunk_size ;
14591458 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 ) ,
14611460 None => self . v . len ( ) ,
14621461 } ;
14631462 let nth_back = & self . v [ start..end] ;
@@ -1579,17 +1578,16 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
15791578 #[ doc( hidden) ]
15801579 unsafe fn __iterator_get_unchecked ( & mut self , idx : usize ) -> Self :: Item {
15811580 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- } ;
15861581 // SAFETY: see comments for `Chunks::__iterator_get_unchecked`.
15871582 //
15881583 // Also note that the caller also guarantees that we're never called
15891584 // with the same index again, and that no other methods that will
15901585 // access this subslice are called, so it is valid for the returned
15911586 // 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+ }
15931591 }
15941592}
15951593
@@ -1619,7 +1617,7 @@ impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
16191617 } else {
16201618 let start = ( len - 1 - n) * self . chunk_size ;
16211619 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 ) ,
16231621 None => self . v . len ( ) ,
16241622 } ;
16251623 let ( temp, _tail) = mem:: replace ( & mut self . v , & mut [ ] ) . split_at_mut ( end) ;
0 commit comments