From 816015ed01330b9c3ab96d21b31ef401b7ce8591 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 7 Jul 2020 09:27:17 +0200 Subject: [PATCH] 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 --- proxmox/src/tools/byte_buffer.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/proxmox/src/tools/byte_buffer.rs b/proxmox/src/tools/byte_buffer.rs index 8ffee6f0..711f2df2 100644 --- a/proxmox/src/tools/byte_buffer.rs +++ b/proxmox/src/tools/byte_buffer.rs @@ -131,11 +131,10 @@ impl ByteBuffer { /// ``` pub fn consume(&mut self, max_amount: usize) -> Box<[u8]> { let size = min(max_amount, self.data_size); - let mut tmp = Vec::with_capacity(size); - tmp.extend_from_slice(&self.buf[..size]); + let tmp: Box<[u8]> = self.buf[..size].into(); self.buf.copy_within(size..self.capacity, 0); self.data_size -= size; - tmp.into_boxed_slice() + tmp } /// Takes a reader and reads into the back of the buffer (up to the