@@ -317,7 +317,8 @@ impl BytesMut {
317
317
) ;
318
318
unsafe {
319
319
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) ;
321
322
self . cap = at;
322
323
self . len = cmp:: min ( self . len , at) ;
323
324
other
@@ -394,7 +395,9 @@ impl BytesMut {
394
395
let mut other = self . shallow_clone ( ) ;
395
396
other. cap = at;
396
397
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) ;
398
401
other
399
402
}
400
403
}
@@ -856,14 +859,19 @@ impl BytesMut {
856
859
unsafe { slice:: from_raw_parts_mut ( self . ptr . as_ptr ( ) , self . len ) }
857
860
}
858
861
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 ) {
860
868
// Setting the start to 0 is a no-op, so return early if this is the
861
869
// case.
862
- if start == 0 {
870
+ if count == 0 {
863
871
return ;
864
872
}
865
873
866
- debug_assert ! ( start <= self . cap, "internal: set_start out of bounds" ) ;
874
+ debug_assert ! ( count <= self . cap, "internal: set_start out of bounds" ) ;
867
875
868
876
let kind = self . kind ( ) ;
869
877
@@ -873,7 +881,7 @@ impl BytesMut {
873
881
// "start" of the byte buffer from the beginning of the vec. We
874
882
// also have to ensure that we don't exceed the maximum shift.
875
883
let ( mut pos, prev) = self . get_vec_pos ( ) ;
876
- pos += start ;
884
+ pos += count ;
877
885
878
886
if pos <= MAX_VEC_POS {
879
887
self . set_vec_pos ( pos, prev) ;
@@ -889,9 +897,9 @@ impl BytesMut {
889
897
// Updating the start of the view is setting `ptr` to point to the
890
898
// new start and updating the `len` field to reflect the new length
891
899
// 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 ;
895
903
}
896
904
897
905
fn try_unsplit ( & mut self , other : BytesMut ) -> Result < ( ) , BytesMut > {
@@ -1062,7 +1070,9 @@ impl Buf for BytesMut {
1062
1070
self . remaining( ) ,
1063
1071
) ;
1064
1072
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) ;
1066
1076
}
1067
1077
}
1068
1078
0 commit comments