Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ unsafe impl BufMut for BytesMut {
Self: Sized,
{
// When capacity is zero, try reusing allocation of `src`.
if self.capacity() == 0 {
if self.capacity() == 0 && src.has_remaining() {
let src_copy = src.copy_to_bytes(src.remaining());
drop(src);
match src_copy.try_into_mut() {
Expand Down Expand Up @@ -1512,6 +1512,12 @@ fn original_capacity_from_repr(repr: usize) -> usize {
mod tests {
use super::*;

#[test]
fn test_bytes_mut_reuse() {
let mut buf = BytesMut::new();
buf.put(&[] as &[u8]);
}

#[test]
fn test_original_capacity_to_repr() {
assert_eq!(original_capacity_to_repr(0), 0);
Expand Down