Add Buf::copy_to_bytes(len) (#439)

This method replaces `Buf::to_bytes()`, providing a method that copies a
subset of the remaining buffer into a `Bytes` value. As this is strictly
more flexible, `to_bytes()` is removed.

Fixes: #129, #398
This commit is contained in:
Carl Lerche
2020-10-20 11:00:35 -07:00
committed by GitHub
parent 5866839e45
commit 39de065a1e
6 changed files with 48 additions and 17 deletions
+8 -2
View File
@@ -548,8 +548,14 @@ impl Buf for Bytes {
}
}
fn to_bytes(&mut self) -> crate::Bytes {
core::mem::replace(self, Bytes::new())
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
if len == self.remaining() {
core::mem::replace(self, Bytes::new())
} else {
let ret = self.slice(..len);
self.advance(len);
ret
}
}
}