ByteBuffer: skip the temporary Vec

The docs say `into_boxed_slice()` "drops" excess capacity,
but doesn't specify whether that just means it becomes
inaccessible or the vector potentially reallocates to the
exact size.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-07-07 09:27:17 +02:00
parent f8ea74dfc1
commit 816015ed01

View File

@ -131,11 +131,10 @@ impl ByteBuffer {
/// ``` /// ```
pub fn consume(&mut self, max_amount: usize) -> Box<[u8]> { pub fn consume(&mut self, max_amount: usize) -> Box<[u8]> {
let size = min(max_amount, self.data_size); let size = min(max_amount, self.data_size);
let mut tmp = Vec::with_capacity(size); let tmp: Box<[u8]> = self.buf[..size].into();
tmp.extend_from_slice(&self.buf[..size]);
self.buf.copy_within(size..self.capacity, 0); self.buf.copy_within(size..self.capacity, 0);
self.data_size -= size; self.data_size -= size;
tmp.into_boxed_slice() tmp
} }
/// Takes a reader and reads into the back of the buffer (up to the /// Takes a reader and reads into the back of the buffer (up to the