pub struct InflateReader<'a> { /* private fields */ }Expand description
Streaming zlib reader: decompresses input incrementally into a small
accumulation buffer, so a caller can parse length-delimited records as they
become available and discard consumed bytes — peak memory stays ~the largest
single record being buffered, not the whole decompressed blob.
Usage: ensure(n) to make ≥ n bytes available, read from available(), then
consume(k). The buffer is compacted (consumed prefix dropped) as it grows.
Implementations§
Source§impl<'a> InflateReader<'a>
impl<'a> InflateReader<'a>
Sourcepub const CHUNK: usize
pub const CHUNK: usize
Output decompress window per pump; also the compaction threshold. One window is the steady state: the pump compacts a consumed prefix before inflating, so the buffer only grows past the chunk when one individual record genuinely exceeds it. Allocated per reader, never retained.
64 KB suits the multi-megabyte history-sync blobs this reader was built
for, where each pump’s fixed cost is worth amortizing; a reader over a
protocol frame on a small heap picks its own with Self::with_chunk.
pub fn new(input: &'a [u8], max: u64) -> Self
Sourcepub fn with_chunk(input: &'a [u8], max: u64, chunk: usize) -> Self
pub fn with_chunk(input: &'a [u8], max: u64, chunk: usize) -> Self
A reader whose output window is chunk bytes instead of Self::CHUNK.
The window is the reader’s one allocation of its own, so it is the reader’s whole footprint beyond the pooled zlib state: a 4 KB chunk keeps a stream of small records inside 4 KB however long the stream is. A record larger than the chunk still grows the window to fit it.
Sourcepub fn ensure(&mut self, need: usize) -> Result<bool>
pub fn ensure(&mut self, need: usize) -> Result<bool>
Ensure at least need unparsed bytes are buffered, decompressing more as
required. Returns Ok(false) if the stream ends before reaching need.
Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
True once the stream is fully decompressed and all bytes consumed.
Sourcepub fn total_out(&self) -> u64
pub fn total_out(&self) -> u64
Total decompressed bytes produced so far. After the stream ends this is the blob’s exact inflated size.
Sourcepub fn compressed_progress(&self) -> (usize, usize)
pub fn compressed_progress(&self) -> (usize, usize)
Compressed input consumed so far plus the input’s full length. The ratio lets callers extrapolate totals (e.g. record counts) from a prefix without a second pass over the blob.
Sourcepub fn stream_ended(&self) -> bool
pub fn stream_ended(&self) -> bool
Whether zlib reported a proper stream end (terminator + adler32
checksum). An EOF (ensure returning false) without this means the
input was truncated, not finished.
Source§impl InflateReader<'_>
impl InflateReader<'_>
Sourcepub fn read_to_end(self) -> Result<Vec<u8>>
pub fn read_to_end(self) -> Result<Vec<u8>>
Inflate the rest of the stream into the window without consuming any of
it, then hand the window over: every decompressed byte from the first
one not yet consumed to the end of the stream, in one Vec.
This is the one-shot decompress_zlib_pooled result reached from a
reader that has already looked at the head of the stream. The window is
grown by the expansion ratio observed so far rather than by doubling, as
the one-shot path does, so the prefix already inflated is what sizes the
rest. A stream that ends without its terminator is an error here, as it
is there: a caller draining to the end wants the whole payload or none.