Skip to main content

StreamDecoder

Struct StreamDecoder 

Source
pub struct StreamDecoder { /* private fields */ }
Expand description

A streaming decoder: feed it input in any pieces, get output in any pieces. Handles concatenated and skippable frames. No std needed; Decoder wraps it as a std::io::Read.

Output is decoded straight into the buffer passed to decode, without going through the decoder’s window buffer, from the start of a frame for as long as the buffer has room for the next block (like libzstd’s single-pass shortcut in ZSTD_decompressStream, but needing neither the whole frame in the input nor its size in the header). An output buffer that holds a whole frame, given at its start, gets it with no copy and no window buffer.

let mut d = zstd_native::StreamDecoder::new();
let mut out = [0u8; 2];
let mut got = Vec::new();
let mut input = compressed;
loop {
    let p = d.decode(input, &mut out)?;
    input = &input[p.read..];
    got.extend_from_slice(&out[..p.written]);
    if input.is_empty() && p.at_frame_boundary {
        break;
    }
}
assert_eq!(got, b"hello");

Implementations§

Source§

impl StreamDecoder

Source

pub fn new() -> Self

A decoder without a dictionary and a 128 MiB (2^27) window limit.

Source

pub fn with_dictionary(dict: Dictionary) -> Self

A decoder that uses dict for every frame.

Source

pub fn set_window_log_max(&mut self, log: u32)

Sets the largest window accepted (frames needing more fail with Error::WindowTooLarge), as 1 << log, log 10 to 31 (30 on 32-bit targets, as libzstd); values outside are clamped. Frames with a window log over 31 (30 on 32-bit) are rejected in every mode, one-shot decoding included. The default is 27 (128 MiB), libzstd’s.

Source

pub fn reset(&mut self)

Forgets any partial frame; the next input must start a frame.

Source

pub fn at_frame_boundary(&self) -> bool

True between frames with no output pending.

Source

pub fn decode( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>

Consumes input and writes output until the input is used up or the output is full.

§Errors

Any Error for corrupt or unsupported input; the decoder must then be reset before reuse. Output written by an erroring call is lost (as libzstd’s), except when the window buffer can’t be allocated (Error::OutputTooLarge) after output already went straight into output: that call returns its progress (not at a frame boundary) and the next call returns the error.

Trait Implementations§

Source§

impl Debug for StreamDecoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StreamDecoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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