Skip to content

BytesMut::freeze ignores advance #352

Closed
@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.

Activity

seanmonstar

seanmonstar commented on Dec 26, 2019

@seanmonstar
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.

added a commit that references this issue on Mar 13, 2020

Fix tokio-rs#352 -- Make freeze respect the start offset for BytesMut…

8259e4b
added a commit that references this issue on Mar 24, 2020

Fix #352 -- Make freeze respect the start offset for BytesMuts in Vec…

8bbe9dd
added a commit that references this issue on Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @seanmonstar@pyfisch

      Issue actions

        BytesMut::freeze ignores advance · Issue #352 · tokio-rs/bytes