Skip to content

Commit 78170db

Browse files
committed
Add Decoder::into_inner for reader recovery without additional input
1 parent 9167d35 commit 78170db

2 files changed

Lines changed: 99 additions & 7 deletions

File tree

‎src/stream/read/mod.rs‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,29 @@ impl<'a, R: BufRead> Decoder<'a, R> {
127127
self.reader.reader_mut()
128128
}
129129

130-
/// Consume the rest of the current frame, so the underlying reader is
131-
/// left pointing just after it.
130+
/// Return the inner reader without consuming additional input.
131+
///
132+
/// This does not read from the underlying reader or try to finish the
133+
/// current frame. Any buffered decoded data is discarded, and the reader
134+
/// is returned at its current position.
135+
///
136+
/// Use [`Self::finish()`] to try to consume any remaining frame input
137+
/// before returning the reader.
138+
pub fn into_inner(self) -> R {
139+
self.reader.into_inner()
140+
}
141+
142+
/// Try to consume the remaining input from the current frame.
132143
///
133144
/// zstd can hand out the last of the decoded data before it has read the
134145
/// frame epilogue, so a reader that stops as soon as it has the bytes it
135146
/// wanted leaves the input somewhere inside the frame. Call this to line
136147
/// the reader back up with the end of the frame - for instance to carry on
137148
/// reading whatever follows the compressed section.
138149
///
150+
/// This does not discard unread decoded data. If there is still output
151+
/// to read, it may stop before reaching the end of the frame.
152+
///
139153
/// This does not touch the underlying reader if the frame is already
140154
/// complete, and never starts decoding the next frame.
141155
pub fn finish_frame(&mut self) -> io::Result<()> {
@@ -147,13 +161,16 @@ impl<'a, R: BufRead> Decoder<'a, R> {
147161
/// Calling `finish()` is not *required* after reading a stream -
148162
/// just use it if you need to get the `Read` back.
149163
///
150-
/// This consumes the rest of the current frame first, so the reader is
151-
/// returned pointing just after it - see [`Self::finish_frame()`]. That
152-
/// may read from the underlying reader, and any error doing so is
153-
/// ignored; call `finish_frame()` directly if you need to see it.
164+
/// This tries to consume any remaining input from the current frame
165+
/// first - see [`Self::finish_frame()`]. That may read from the underlying
166+
/// reader, and any error doing so is ignored; call `finish_frame()`
167+
/// directly if you need to see it.
168+
///
169+
/// To return the reader without consuming additional input, use
170+
/// [`Self::into_inner()`].
154171
pub fn finish(mut self) -> R {
155172
let _ = self.finish_frame();
156-
self.reader.into_inner()
173+
self.into_inner()
157174
}
158175

159176
crate::decoder_common!(reader);

‎tests/issue_381.rs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//! Recovering a decoder's reader must not consume compressed input.
2+
//!
3+
//! https://github.com/gyscos/zstd-rs/issues/381
4+
5+
use std::cell::Cell;
6+
use std::io::{self, BufRead, Cursor, Read};
7+
8+
struct Counting<R> {
9+
inner: R,
10+
fill_buf_calls: Cell<usize>,
11+
}
12+
13+
impl<R: Read> Read for Counting<R> {
14+
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
15+
self.inner.read(output)
16+
}
17+
}
18+
19+
impl<R: BufRead> BufRead for Counting<R> {
20+
fn fill_buf(&mut self) -> io::Result<&[u8]> {
21+
self.fill_buf_calls.set(self.fill_buf_calls.get() + 1);
22+
self.inner.fill_buf()
23+
}
24+
25+
fn consume(&mut self, amount: usize) {
26+
self.inner.consume(amount);
27+
}
28+
}
29+
30+
#[test]
31+
fn into_inner_preserves_an_unread_frame_for_raw_copying() {
32+
let payload = vec![0u8; 64];
33+
let frame = zstd::encode_all(&payload[..], 3).unwrap();
34+
let source = Counting {
35+
inner: Cursor::new(&frame),
36+
fill_buf_calls: Cell::new(0),
37+
};
38+
let decoder = zstd::Decoder::with_buffer(source).unwrap();
39+
40+
let mut reader = decoder.into_inner();
41+
assert_eq!(reader.inner.position(), 0);
42+
assert_eq!(reader.fill_buf_calls.get(), 0);
43+
44+
let mut copied = Vec::new();
45+
io::copy(&mut reader, &mut copied).unwrap();
46+
assert_eq!(copied, frame);
47+
assert_eq!(zstd::decode_all(&copied[..]).unwrap(), payload);
48+
}
49+
50+
#[test]
51+
fn into_inner_does_not_fill_an_unread_bufreader() {
52+
let frame = zstd::encode_all(&b"raw copy"[..], 3).unwrap();
53+
let decoder = zstd::Decoder::new(Cursor::new(&frame)).unwrap();
54+
55+
let reader = decoder.into_inner();
56+
assert_eq!(reader.get_ref().position(), 0);
57+
assert!(reader.buffer().is_empty());
58+
}
59+
60+
#[test]
61+
fn into_inner_preserves_the_position_after_a_partial_read() {
62+
let frame = zstd::encode_all(&vec![0u8; 1 << 20][..], 3).unwrap();
63+
let source = Counting {
64+
inner: Cursor::new(&frame),
65+
fill_buf_calls: Cell::new(0),
66+
};
67+
let mut decoder = zstd::Decoder::with_buffer(source).unwrap();
68+
decoder.read_exact(&mut [0u8; 4]).unwrap();
69+
let position = decoder.get_ref().inner.position();
70+
let calls = decoder.get_ref().fill_buf_calls.get();
71+
72+
let reader = decoder.into_inner();
73+
assert_eq!(reader.inner.position(), position);
74+
assert_eq!(reader.fill_buf_calls.get(), calls);
75+
}

0 commit comments

Comments
 (0)