diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 0248df856..f707331bd 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -1064,6 +1064,17 @@ impl Buf for BytesMut { cnt, self.remaining(), ); + + // 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.len { + // SAFETY: Zero is not greater than the capacity. + unsafe { + self.set_len(0); + } + return; + } + unsafe { // SAFETY: We've checked that `cnt` <= `self.remaining()` and we know that // `self.remaining()` <= `self.cap`.