Skip to content

Commit

Permalink
Implement accessors for inner reader on StreamDecoder (#62)
Browse files Browse the repository at this point in the history
* Implement accessors for inner reader on StreamDecoder
  • Loading branch information
ifd3f committed May 29, 2024
1 parent 0b96073 commit 16fee8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
33 changes: 32 additions & 1 deletion src/streaming_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,39 @@ impl<READ: Read> StreamingDecoder<READ, FrameDecoder> {
decoder.init(&mut source)?;
Ok(StreamingDecoder { decoder, source })
}
}

impl<READ: Read, DEC: BorrowMut<FrameDecoder>> StreamingDecoder<READ, DEC> {
/// Gets a reference to the underlying reader.
pub fn get_ref(&self) -> &READ {
&self.source
}

/// Gets a mutable reference to the underlying reader.
///
/// It is inadvisable to directly read from the underlying reader.
pub fn get_mut(&mut self) -> &mut READ {
&mut self.source
}

/// Destructures this object into the inner reader.
pub fn into_inner(self) -> READ
where
READ: Sized,
{
self.source
}

/// Destructures this object into both the inner reader and [FrameDecoder].
pub fn into_parts(self) -> (READ, DEC)
where
READ: Sized,
{
(self.source, self.decoder)
}

pub fn inner(self) -> FrameDecoder {
/// Destructures this object into the inner [FrameDecoder].
pub fn into_frame_decoder(self) -> DEC {
self.decoder
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ fn test_streaming() {
// Test resetting to a new file while keeping the old decoder

let mut content = fs::File::open("./decodecorpus_files/z000068.zst").unwrap();
let mut stream =
crate::streaming_decoder::StreamingDecoder::new_with_decoder(&mut content, stream.inner())
.unwrap();
let mut stream = crate::streaming_decoder::StreamingDecoder::new_with_decoder(
&mut content,
stream.into_frame_decoder(),
)
.unwrap();

let mut result = Vec::new();
Read::read_to_end(&mut stream, &mut result).unwrap();
Expand Down Expand Up @@ -415,9 +417,11 @@ fn test_streaming_no_std() {

let content = include_bytes!("../../decodecorpus_files/z000068.zst");
let mut content = content.as_slice();
let mut stream =
crate::streaming_decoder::StreamingDecoder::new_with_decoder(&mut content, stream.inner())
.unwrap();
let mut stream = crate::streaming_decoder::StreamingDecoder::new_with_decoder(
&mut content,
stream.into_frame_decoder(),
)
.unwrap();

let original = include_bytes!("../../decodecorpus_files/z000068");
let mut result = vec![0; original.len()];
Expand Down

0 comments on commit 16fee8c

Please sign in to comment.