Truncate tweaks (#694)

This commit is contained in:
Brad Dunbar
2024-04-17 11:27:00 +02:00
committed by GitHub
parent 327615e5d4
commit 4e2c9c065a
+2 -1
View File
@@ -422,8 +422,9 @@ impl BytesMut {
/// assert_eq!(buf, b"hello"[..]); /// assert_eq!(buf, b"hello"[..]);
/// ``` /// ```
pub fn truncate(&mut self, len: usize) { pub fn truncate(&mut self, len: usize) {
if len <= self.len() { if len < self.len() {
unsafe { unsafe {
// SAFETY: Shrinking the buffer cannot expose uninitialized bytes.
self.set_len(len); self.set_len(len);
} }
} }