@@ -317,7 +317,8 @@ impl BytesMut {
317317 ) ;
318318 unsafe {
319319 let mut other = self . shallow_clone ( ) ;
320- other. set_start ( at) ;
320+ // SAFETY: We've checked that `at` <= `self.capacity()` above.
321+ other. advance_unchecked ( at) ;
321322 self . cap = at;
322323 self . len = cmp:: min ( self . len , at) ;
323324 other
@@ -394,7 +395,9 @@ impl BytesMut {
394395 let mut other = self . shallow_clone ( ) ;
395396 other. cap = at;
396397 other. len = at;
397- self . set_start ( at) ;
398+ // SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
399+ // `self.capacity()`.
400+ self . advance_unchecked ( at) ;
398401 other
399402 }
400403 }
@@ -856,14 +859,19 @@ impl BytesMut {
856859 unsafe { slice:: from_raw_parts_mut ( self . ptr . as_ptr ( ) , self . len ) }
857860 }
858861
859- unsafe fn set_start ( & mut self , start : usize ) {
862+ /// Advance the buffer without bounds checking.
863+ ///
864+ /// # SAFETY
865+ ///
866+ /// The caller must ensure that `count` <= `self.cap`.
867+ unsafe fn advance_unchecked ( & mut self , count : usize ) {
860868 // Setting the start to 0 is a no-op, so return early if this is the
861869 // case.
862- if start == 0 {
870+ if count == 0 {
863871 return ;
864872 }
865873
866- debug_assert ! ( start <= self . cap, "internal: set_start out of bounds" ) ;
874+ debug_assert ! ( count <= self . cap, "internal: set_start out of bounds" ) ;
867875
868876 let kind = self . kind ( ) ;
869877
@@ -873,7 +881,7 @@ impl BytesMut {
873881 // "start" of the byte buffer from the beginning of the vec. We
874882 // also have to ensure that we don't exceed the maximum shift.
875883 let ( mut pos, prev) = self . get_vec_pos ( ) ;
876- pos += start ;
884+ pos += count ;
877885
878886 if pos <= MAX_VEC_POS {
879887 self . set_vec_pos ( pos, prev) ;
@@ -889,9 +897,9 @@ impl BytesMut {
889897 // Updating the start of the view is setting `ptr` to point to the
890898 // new start and updating the `len` field to reflect the new length
891899 // of the view.
892- self . ptr = vptr ( self . ptr . as_ptr ( ) . add ( start ) ) ;
893- self . len = self . len . checked_sub ( start ) . unwrap_or ( 0 ) ;
894- self . cap -= start ;
900+ self . ptr = vptr ( self . ptr . as_ptr ( ) . add ( count ) ) ;
901+ self . len = self . len . checked_sub ( count ) . unwrap_or ( 0 ) ;
902+ self . cap -= count ;
895903 }
896904
897905 fn try_unsplit ( & mut self , other : BytesMut ) -> Result < ( ) , BytesMut > {
@@ -1062,7 +1070,9 @@ impl Buf for BytesMut {
10621070 self . remaining( ) ,
10631071 ) ;
10641072 unsafe {
1065- self . set_start ( cnt) ;
1073+ // SAFETY: We've checked that `cnt` <= `self.remaining()` and we know that
1074+ // `self.remaining()` <= `self.cap`.
1075+ self . advance_unchecked ( cnt) ;
10661076 }
10671077 }
10681078
0 commit comments