Closed
Description
Hi,
I created a BytesBuf
with some data and removed the leading n bytes with advance(n)
. After some more computations I converted the result to an immutable Bytes
value. My expectation was that the content of the BytesMut
and Bytes
would be identical. Instead the Bytes
still contained the discared bytes.
Example program:
use bytes::{BytesMut, Buf, BufMut};
fn main() {
let mut x = BytesMut::new();
x.put(&b"hello"[..]);
let mut y = BytesMut::new();
y.put(&b"hello"[..]);
y.advance(2);
println!("{:?} {:?}", x, y); // b"hello" b"llo"
assert_ne!(x, y);
assert_ne!(&x[..], &y[..]);
let x1 = x.freeze();
let y1 = y.freeze();
println!("{:?} {:?}", x1, y1); // b"hello" b"hello"
// expected: b"hello" b"llo"
assert_eq!(x1, y1);
assert_eq!(&x1[..], &y1[..]);
}
As a work-around I use split_to
instead of advance
and ignore the return value.
Activity
seanmonstar commentedon Dec 26, 2019
Oh yea, that seems like a bug in
freeze
. Looking at the source, it seems to grab the original size so it can properly create aBytes
, but forgets to advance it forward if need be.Fix tokio-rs#352 -- Make freeze respect the start offset for BytesMut…
Fix #352 -- Make freeze respect the start offset for BytesMuts in Vec…
chore(deps): update all dependencies (#2)