Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BytesMut::freeze ignores advance #352

Closed
pyfisch opened this issue Dec 26, 2019 · 1 comment · Fixed by #376
Closed

BytesMut::freeze ignores advance #352

pyfisch opened this issue Dec 26, 2019 · 1 comment · Fixed by #376
Labels

Comments

@pyfisch
Copy link

pyfisch commented Dec 26, 2019

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[..]);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ffaabad67256d03d04f6478d60b93381

As a work-around I use split_to instead of advance and ignore the return value.

@seanmonstar
Copy link
Member

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 a Bytes, but forgets to advance it forward if need be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants