diff options
Diffstat (limited to 'vendor/ruzstd/src/streaming_decoder.rs')
-rw-r--r-- | vendor/ruzstd/src/streaming_decoder.rs | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/vendor/ruzstd/src/streaming_decoder.rs b/vendor/ruzstd/src/streaming_decoder.rs index 2613a363b..f345a897c 100644 --- a/vendor/ruzstd/src/streaming_decoder.rs +++ b/vendor/ruzstd/src/streaming_decoder.rs @@ -1,27 +1,33 @@ +use core::borrow::BorrowMut; + use crate::frame_decoder::{BlockDecodingStrategy, FrameDecoder, FrameDecoderError}; -use std::io::Read; +use crate::io::{Error, ErrorKind, Read}; /// High level decoder that implements a io::Read that can be used with /// io::Read::read_to_end / io::Read::read_exact or passing this to another library / module as a source for the decoded content /// /// The lower level FrameDecoder by comparison allows for finer grained control but need sto have it's decode_blocks method called continously /// to decode the zstd-frame. -pub struct StreamingDecoder<READ: Read> { - pub decoder: FrameDecoder, +pub struct StreamingDecoder<READ: Read, DEC: BorrowMut<FrameDecoder>> { + pub decoder: DEC, source: READ, } -impl<READ: Read> StreamingDecoder<READ> { - pub fn new(mut source: READ) -> Result<StreamingDecoder<READ>, FrameDecoderError> { - let mut decoder = FrameDecoder::new(); - decoder.init(&mut source)?; +impl<READ: Read, DEC: BorrowMut<FrameDecoder>> StreamingDecoder<READ, DEC> { + pub fn new_with_decoder( + mut source: READ, + mut decoder: DEC, + ) -> Result<StreamingDecoder<READ, DEC>, FrameDecoderError> { + decoder.borrow_mut().init(&mut source)?; Ok(StreamingDecoder { decoder, source }) } +} - pub fn new_with_decoder( +impl<READ: Read> StreamingDecoder<READ, FrameDecoder> { + pub fn new( mut source: READ, - mut decoder: FrameDecoder, - ) -> Result<StreamingDecoder<READ>, FrameDecoderError> { + ) -> Result<StreamingDecoder<READ, FrameDecoder>, FrameDecoderError> { + let mut decoder = FrameDecoder::new(); decoder.init(&mut source)?; Ok(StreamingDecoder { decoder, source }) } @@ -31,9 +37,10 @@ impl<READ: Read> StreamingDecoder<READ> { } } -impl<READ: Read> Read for StreamingDecoder<READ> { - fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { - if self.decoder.is_finished() && self.decoder.can_collect() == 0 { +impl<READ: Read, DEC: BorrowMut<FrameDecoder>> Read for StreamingDecoder<READ, DEC> { + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { + let decoder = self.decoder.borrow_mut(); + if decoder.is_finished() && decoder.can_collect() == 0 { //No more bytes can ever be decoded return Ok(0); } @@ -43,24 +50,24 @@ impl<READ: Read> Read for StreamingDecoder<READ> { // So we need to call this until we can actually collect enough bytes // TODO add BlockDecodingStrategy::UntilCollectable(usize) that pushes this logic into the decode_blocks function - while self.decoder.can_collect() < buf.len() && !self.decoder.is_finished() { + while decoder.can_collect() < buf.len() && !decoder.is_finished() { //More bytes can be decoded - let additional_bytes_needed = buf.len() - self.decoder.can_collect(); - match self.decoder.decode_blocks( + let additional_bytes_needed = buf.len() - decoder.can_collect(); + match decoder.decode_blocks( &mut self.source, BlockDecodingStrategy::UptoBytes(additional_bytes_needed), ) { Ok(_) => { /*Nothing to do*/ } Err(e) => { - let err = std::io::Error::new( - std::io::ErrorKind::Other, - format!("Error in the zstd decoder: {:?}", e), + let err = Error::new( + ErrorKind::Other, + alloc::format!("Error in the zstd decoder: {:?}", e), ); return Err(err); } } } - self.decoder.read(buf) + decoder.read(buf) } } |