Skip to main content

InflateReader

Struct InflateReader 

Source
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>

Source

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.

Source

pub fn new(input: &'a [u8], max: u64) -> Self

Source

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.

Source

pub fn available(&self) -> &[u8] ⓘ

Unparsed decompressed bytes currently buffered.

Source

pub fn consume(&mut self, n: usize)

Mark n already-read bytes as consumed.

Source

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.

Source

pub fn is_done(&self) -> bool

True once the stream is fully decompressed and all bytes consumed.

Source

pub fn total_out(&self) -> u64

Total decompressed bytes produced so far. After the stream ends this is the blob’s exact inflated size.

Source

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.

Source

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<'_>

Source

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.

Trait Implementations§

Source§

impl Drop for InflateReader<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for InflateReader<'a>

§

impl<'a> Forget for InflateReader<'a>

§

impl<'a> Freeze for InflateReader<'a>
where &'a [u8]: Freeze, *mut u8: Freeze, *mut i8: Freeze, &'static mut State<'static>: Freeze, *mut c_void: Freeze, PhantomData<&'static ()>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl<'a> Move for InflateReader<'a>

§

impl<'a> RefUnwindSafe for InflateReader<'a>

§

impl<'a> Send for InflateReader<'a>
where &'a [u8]: Send, InflateStream<'static>: Send, RawVecInner: Send,

§

impl<'a> Sync for InflateReader<'a>
where InflateStream<'static>: Sync, RawVecInner: Sync,

§

impl<'a> Unpin for InflateReader<'a>
where &'a [u8]: Unpin, *mut u8: Unpin, *mut i8: Unpin, &'static mut State<'static>: Unpin, *mut c_void: Unpin, &'static (): Unpin, *const u8 is !null: Unpin,

§

impl<'a> UnsafeUnpin for InflateReader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.