File tree Expand file tree Collapse file tree 1 file changed +6
-8
lines changed Expand file tree Collapse file tree 1 file changed +6
-8
lines changed Original file line number Diff line number Diff line change @@ -422,11 +422,9 @@ impl BytesMut {
422422 /// assert_eq!(buf, b"hello"[..]);
423423 /// ```
424424 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) } ;
430428 }
431429 }
432430
@@ -442,7 +440,8 @@ impl BytesMut {
442440 /// assert!(buf.is_empty());
443441 /// ```
444442 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 ) } ;
446445 }
447446
448447 /// Resizes the buffer so that `len` is equal to `new_len`.
@@ -1069,8 +1068,7 @@ impl Buf for BytesMut {
10691068 // Advancing by the length is the same as resetting the length to 0,
10701069 // except this way we get to reuse the full capacity.
10711070 if cnt == self . remaining ( ) {
1072- // SAFETY: Zero is not greater than the capacity.
1073- unsafe { self . set_len ( 0 ) } ;
1071+ self . clear ( ) ;
10741072 return ;
10751073 }
10761074
You can’t perform that action at this time.
0 commit comments