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:
Stepan Koltsov
2021-04-11 03:56:35 +09:00
committed by GitHub
parent 3d5624a452
commit b9eade12a5
4 changed files with 69 additions and 2 deletions
+9 -1
View File
@@ -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
}
}