Skip to content

BytesMut::freeze ignores advance #352

@pyfisch

Description

@pyfisch

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions