diff --git a/src/collect.rs b/src/collect.rs index b0b0f0d..0d0d9ae 100644 --- a/src/collect.rs +++ b/src/collect.rs @@ -209,9 +209,15 @@ impl Buf for BufList { } Some(front) if front.remaining() > len => front.copy_to_bytes(len), _ => { - assert!(len <= self.remaining(), "`len` greater than remaining"); + let rem = self.remaining(); + assert!(len <= rem, "`len` greater than remaining"); let mut bm = BytesMut::with_capacity(len); - bm.put(self.take(len)); + if rem == len { + // .take() costs a lot more, so skip it if we don't need it + bm.put(self); + } else { + bm.put(self.take(len)); + } bm.freeze() } }