Skip to content

Commit

Permalink
Keep capacity when unsplit on empty other buf (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gbillou authored Jul 5, 2021
1 parent ed1d24e commit baaf12d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ impl BytesMut {
}

fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> {
if other.is_empty() {
if other.capacity() == 0 {
return Ok(());
}

Expand Down
25 changes: 25 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,31 @@ fn bytes_mut_unsplit_empty_self() {
assert_eq!(b"aaabbbcccddd", &buf[..]);
}

#[test]
fn bytes_mut_unsplit_other_keeps_capacity() {
let mut buf = BytesMut::with_capacity(64);
buf.extend_from_slice(b"aabb");

// non empty other created "from" buf
let mut other = buf.split_off(buf.len());
other.extend_from_slice(b"ccddee");
buf.unsplit(other);

assert_eq!(buf.capacity(), 64);
}

#[test]
fn bytes_mut_unsplit_empty_other_keeps_capacity() {
let mut buf = BytesMut::with_capacity(64);
buf.extend_from_slice(b"aabbccddee");

// empty other created "from" buf
let other = buf.split_off(buf.len());
buf.unsplit(other);

assert_eq!(buf.capacity(), 64);
}

#[test]
fn bytes_mut_unsplit_arc_different() {
let mut buf = BytesMut::with_capacity(64);
Expand Down

0 comments on commit baaf12d

Please sign in to comment.