bytes icon indicating copy to clipboard operation
bytes copied to clipboard

Unsplit an empty but with non-zero capacity `BytesMut` doesn't work

Open songzhi opened this issue 4 years ago • 0 comments

let mut bytes = BytesMut::with_capacity(32);
unsafe {
    bytes.set_len(16);
}
let mut rest = bytes.split_off(16);
// unsafe {
//     rest.set_len(8);
// }
println!("{}", rest.capacity()); // 16
bytes.unsplit(rest);
println!("{}", bytes.capacity()); // 16, but it should be 32

if I uncomment rest.set_len(8), the result would be right. I think the following logic is wrong. It just check length, the capacity should be checked too.

https://github.com/tokio-rs/bytes/blob/6fdb7391ce83dc71ccaeda6a54211ce723e5d9a5/src/bytes_mut.rs#L821-L824

songzhi avatar Aug 08 '20 08:08 songzhi