File tree 1 file changed +6
-8
lines changed
1 file changed +6
-8
lines changed Original file line number Diff line number Diff line change @@ -422,11 +422,9 @@ impl BytesMut {
422
422
/// assert_eq!(buf, b"hello"[..]);
423
423
/// ```
424
424
pub fn truncate ( & mut self , len : usize ) {
425
- if len < self . len ( ) {
426
- unsafe {
427
- // SAFETY: Shrinking the buffer cannot expose uninitialized bytes.
428
- self . set_len ( len) ;
429
- }
425
+ if len <= self . len ( ) {
426
+ // SAFETY: Shrinking the buffer cannot expose uninitialized bytes.
427
+ unsafe { self . set_len ( len) } ;
430
428
}
431
429
}
432
430
@@ -442,7 +440,8 @@ impl BytesMut {
442
440
/// assert!(buf.is_empty());
443
441
/// ```
444
442
pub fn clear ( & mut self ) {
445
- self . truncate ( 0 ) ;
443
+ // SAFETY: Setting the length to zero cannot expose uninitialized bytes.
444
+ unsafe { self . set_len ( 0 ) } ;
446
445
}
447
446
448
447
/// Resizes the buffer so that `len` is equal to `new_len`.
@@ -1069,8 +1068,7 @@ impl Buf for BytesMut {
1069
1068
// Advancing by the length is the same as resetting the length to 0,
1070
1069
// except this way we get to reuse the full capacity.
1071
1070
if cnt == self . remaining ( ) {
1072
- // SAFETY: Zero is not greater than the capacity.
1073
- unsafe { self . set_len ( 0 ) } ;
1071
+ self . clear ( ) ;
1074
1072
return ;
1075
1073
}
1076
1074
You can’t perform that action at this time.
0 commit comments