Description
The following test fails with bytes 0.5.5:
#[test]
fn bytes_mut_reuse() {
use bytes::{BytesMut, Buf};
let mut buf = BytesMut::new();
buf.reserve(8192);
buf.extend_from_slice(&[0u8;100][..]);
let p = &buf[0] as *const u8;
buf.advance(100);
buf.reserve(8192);
buf.extend_from_slice(b" ");
assert_eq!(&buf[0] as *const u8, p);
}
I would expect this test to pass, as we should be able to reuse the entire buffer given that there is only one reference, and that since we've consumed the entire buffer there should be no data to copy.
The issue appears to come from this condition here:
https://github.com/tokio-rs/bytes/blob/master/src/bytes_mut.rs#L562-L564
Here, additional
refers to the value passed to reserve - this is the amount of data above the current length, but here we require that the current offset be beyond that, for some reason - perhaps this is meant to be checking against additional - capacity
? Additionally, this does not take into account the amount of data that we need to copy - if self.len() == 0
then the copy is free and we should always take this option.