mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-07 00:00:13 +02:00
Reuse capacity when possible in <BytesMut as Buf>::advance impl (#698)
This commit is contained in:
@@ -1066,6 +1066,14 @@ impl Buf for BytesMut {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn advance(&mut self, cnt: usize) {
|
fn advance(&mut self, cnt: usize) {
|
||||||
|
// 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) };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
cnt <= self.remaining(),
|
cnt <= self.remaining(),
|
||||||
"cannot advance past `remaining`: {:?} <= {:?}",
|
"cannot advance past `remaining`: {:?} <= {:?}",
|
||||||
|
|||||||
Reference in New Issue
Block a user