Move ReadBuffer chunk to heap

This commit is contained in:
PhotonQuantum 2021-10-15 07:12:53 +08:00
parent 7a359f0b4f
commit 3f5d0985f0
No known key found for this signature in database
GPG Key ID: 488E05BFDD880F00
1 changed files with 3 additions and 3 deletions

View File

@ -12,7 +12,7 @@ use bytes::Buf;
#[derive(Debug)]
pub struct ReadBuffer<const CHUNK_SIZE: usize> {
storage: Cursor<Vec<u8>>,
chunk: [u8; CHUNK_SIZE],
chunk: Box<[u8; CHUNK_SIZE]>,
}
impl<const CHUNK_SIZE: usize> ReadBuffer<CHUNK_SIZE> {
@ -28,7 +28,7 @@ impl<const CHUNK_SIZE: usize> ReadBuffer<CHUNK_SIZE> {
/// Create a input buffer filled with previously read data.
pub fn from_partially_read(part: Vec<u8>) -> Self {
Self { storage: Cursor::new(part), chunk: [0; CHUNK_SIZE] }
Self { storage: Cursor::new(part), chunk: Box::new([0; CHUNK_SIZE]) }
}
/// Get a cursor to the data storage.
@ -54,7 +54,7 @@ impl<const CHUNK_SIZE: usize> ReadBuffer<CHUNK_SIZE> {
/// Read next portion of data from the given input stream.
pub fn read_from<S: Read>(&mut self, stream: &mut S) -> IoResult<usize> {
self.clean_up();
let size = stream.read(&mut self.chunk)?;
let size = stream.read(&mut *self.chunk)?;
self.storage.get_mut().extend_from_slice(&self.chunk[..size]);
Ok(size)
}