Skip to content

Commit

Permalink
Tweak clear and truncate length modifications (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Apr 26, 2024
1 parent a8806c2 commit cb7f844
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/bytes_mut.rs
Expand Up @@ -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) };
}
}

Expand All @@ -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`.
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit cb7f844

Please sign in to comment.