mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-16 00:00:15 +02:00
Specialize copy_to_bytes for Chain and Take (#481)
Avoid allocation when `Take` or `Chain` is composed of `Bytes` objects. This works now for `Take`. `Chain` it works if the requested bytes does not cross boundary between `Chain` members.
This commit is contained in:
+9
-1
@@ -1,4 +1,4 @@
|
||||
use crate::Buf;
|
||||
use crate::{Buf, Bytes};
|
||||
|
||||
use core::cmp;
|
||||
|
||||
@@ -144,4 +144,12 @@ impl<T: Buf> Buf for Take<T> {
|
||||
self.inner.advance(cnt);
|
||||
self.limit -= cnt;
|
||||
}
|
||||
|
||||
fn copy_to_bytes(&mut self, len: usize) -> Bytes {
|
||||
assert!(len <= self.remaining(), "`len` greater than remaining");
|
||||
|
||||
let r = self.inner.copy_to_bytes(len);
|
||||
self.limit -= len;
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user