Skip to content

Commit baaf12d

Browse files
authored
Keep capacity when unsplit on empty other buf (#502)
1 parent ed1d24e commit baaf12d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/bytes_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ impl BytesMut {
819819
}
820820

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

tests/test_bytes.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,31 @@ fn bytes_mut_unsplit_empty_self() {
784784
assert_eq!(b"aaabbbcccddd", &buf[..]);
785785
}
786786

787+
#[test]
788+
fn bytes_mut_unsplit_other_keeps_capacity() {
789+
let mut buf = BytesMut::with_capacity(64);
790+
buf.extend_from_slice(b"aabb");
791+
792+
// non empty other created "from" buf
793+
let mut other = buf.split_off(buf.len());
794+
other.extend_from_slice(b"ccddee");
795+
buf.unsplit(other);
796+
797+
assert_eq!(buf.capacity(), 64);
798+
}
799+
800+
#[test]
801+
fn bytes_mut_unsplit_empty_other_keeps_capacity() {
802+
let mut buf = BytesMut::with_capacity(64);
803+
buf.extend_from_slice(b"aabbccddee");
804+
805+
// empty other created "from" buf
806+
let other = buf.split_off(buf.len());
807+
buf.unsplit(other);
808+
809+
assert_eq!(buf.capacity(), 64);
810+
}
811+
787812
#[test]
788813
fn bytes_mut_unsplit_arc_different() {
789814
let mut buf = BytesMut::with_capacity(64);

0 commit comments

Comments
 (0)