@@ -317,8 +317,10 @@ impl BytesMut {
317
317
) ;
318
318
unsafe {
319
319
let mut other = self . shallow_clone ( ) ;
320
- other. set_start ( at) ;
321
- self . set_end ( at) ;
320
+ // SAFETY: We've checked that `at` <= `self.capacity()` above.
321
+ other. advance_unchecked ( at) ;
322
+ self . cap = at;
323
+ self . len = cmp:: min ( self . len , at) ;
322
324
other
323
325
}
324
326
}
@@ -391,8 +393,11 @@ impl BytesMut {
391
393
392
394
unsafe {
393
395
let mut other = self . shallow_clone ( ) ;
394
- other. set_end ( at) ;
395
- self . set_start ( at) ;
396
+ // SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
397
+ // `self.capacity()`.
398
+ self . advance_unchecked ( at) ;
399
+ other. cap = at;
400
+ other. len = at;
396
401
other
397
402
}
398
403
}
@@ -851,14 +856,19 @@ impl BytesMut {
851
856
unsafe { slice:: from_raw_parts_mut ( self . ptr . as_ptr ( ) , self . len ) }
852
857
}
853
858
854
- unsafe fn set_start ( & mut self , start : usize ) {
859
+ /// Advance the buffer without bounds checking.
860
+ ///
861
+ /// # SAFETY
862
+ ///
863
+ /// The caller must ensure that `count` <= `self.cap`.
864
+ unsafe fn advance_unchecked ( & mut self , count : usize ) {
855
865
// Setting the start to 0 is a no-op, so return early if this is the
856
866
// case.
857
- if start == 0 {
867
+ if count == 0 {
858
868
return ;
859
869
}
860
870
861
- debug_assert ! ( start <= self . cap, "internal: set_start out of bounds" ) ;
871
+ debug_assert ! ( count <= self . cap, "internal: set_start out of bounds" ) ;
862
872
863
873
let kind = self . kind ( ) ;
864
874
@@ -867,7 +877,7 @@ impl BytesMut {
867
877
// complicated. First, we have to track how far ahead the
868
878
// "start" of the byte buffer from the beginning of the vec. We
869
879
// also have to ensure that we don't exceed the maximum shift.
870
- let pos = self . get_vec_pos ( ) + start ;
880
+ let pos = self . get_vec_pos ( ) + count ;
871
881
872
882
if pos <= MAX_VEC_POS {
873
883
self . set_vec_pos ( pos) ;
@@ -883,23 +893,9 @@ impl BytesMut {
883
893
// Updating the start of the view is setting `ptr` to point to the
884
894
// new start and updating the `len` field to reflect the new length
885
895
// of the view.
886
- self . ptr = vptr ( self . ptr . as_ptr ( ) . add ( start) ) ;
887
-
888
- if self . len >= start {
889
- self . len -= start;
890
- } else {
891
- self . len = 0 ;
892
- }
893
-
894
- self . cap -= start;
895
- }
896
-
897
- unsafe fn set_end ( & mut self , end : usize ) {
898
- debug_assert_eq ! ( self . kind( ) , KIND_ARC ) ;
899
- assert ! ( end <= self . cap, "set_end out of bounds" ) ;
900
-
901
- self . cap = end;
902
- self . len = cmp:: min ( self . len , end) ;
896
+ self . ptr = vptr ( self . ptr . as_ptr ( ) . add ( count) ) ;
897
+ self . len = self . len . checked_sub ( count) . unwrap_or ( 0 ) ;
898
+ self . cap -= count;
903
899
}
904
900
905
901
fn try_unsplit ( & mut self , other : BytesMut ) -> Result < ( ) , BytesMut > {
@@ -1069,7 +1065,9 @@ impl Buf for BytesMut {
1069
1065
self . remaining( ) ,
1070
1066
) ;
1071
1067
unsafe {
1072
- self . set_start ( cnt) ;
1068
+ // SAFETY: We've checked that `cnt` <= `self.remaining()` and we know that
1069
+ // `self.remaining()` <= `self.cap`.
1070
+ self . advance_unchecked ( cnt) ;
1073
1071
}
1074
1072
}
1075
1073
0 commit comments