diff options
author | 2024-04-26 09:24:05 +0200 | |
---|---|---|
committer | 2024-04-26 09:24:05 +0200 | |
commit | cb7f8449b5efc7022dc592b3a1d7dd33079f4c8f (patch) | |
tree | 749e17eb316d11014b0387c0c07ab10aa9a93083 | |
parent | a8806c245700e583134e67b7e0b87f1256b95bfa (diff) | |
download | bytes-cb7f8449b5efc7022dc592b3a1d7dd33079f4c8f.tar.gz bytes-cb7f8449b5efc7022dc592b3a1d7dd33079f4c8f.tar.zst bytes-cb7f8449b5efc7022dc592b3a1d7dd33079f4c8f.zip |
Tweak clear and truncate length modifications (#700)
-rw-r--r-- | src/bytes_mut.rs | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 7576299..b01bb1a 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -422,11 +422,9 @@ impl BytesMut { /// assert_eq!(buf, b"hello"[..]); /// ``` pub fn truncate(&mut self, len: usize) { - if len < self.len() { - unsafe { - // SAFETY: Shrinking the buffer cannot expose uninitialized bytes. - self.set_len(len); - } + if len <= self.len() { + // SAFETY: Shrinking the buffer cannot expose uninitialized bytes. + unsafe { self.set_len(len) }; } } @@ -442,7 +440,8 @@ impl BytesMut { /// assert!(buf.is_empty()); /// ``` pub fn clear(&mut self) { - self.truncate(0); + // SAFETY: Setting the length to zero cannot expose uninitialized bytes. + unsafe { self.set_len(0) }; } /// Resizes the buffer so that `len` is equal to `new_len`. @@ -1069,8 +1068,7 @@ impl Buf for BytesMut { // Advancing by the length is the same as resetting the length to 0, // except this way we get to reuse the full capacity. if cnt == self.remaining() { - // SAFETY: Zero is not greater than the capacity. - unsafe { self.set_len(0) }; + self.clear(); return; } |