Skip to main content

StreamEncoder

Struct StreamEncoder 

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

Streaming Zstandard encoder (ZSTD_compressStream2 semantics).

Output produced by a given sequence of compress, flush and finish calls is byte-identical to C libzstd 1.5.7 fed the same input chunks with the same ZSTD_e_continue / ZSTD_e_flush / ZSTD_e_end directives.

let mut out = Vec::new();
let mut enc = libzstd_bitexact_rs::StreamEncoder::new(3);
enc.compress(b"hello ", &mut out).unwrap();
enc.compress(b"world", &mut out).unwrap();
enc.finish(b"", &mut out).unwrap();
assert_eq!(libzstd_bitexact_rs::decompress(&out).unwrap(), b"hello world");

Implementations§

Source§

impl StreamEncoder

Source

pub fn new(level: i32) -> Self

A streaming encoder with unknown content size (the ZSTD_compressStream2 default).

Source

pub fn with_dictionary(level: i32, dict: &[u8]) -> Self

A streaming encoder primed with a dictionary (ZSTD_CCtx_loadDictionary semantics: an internal CDict is built and attached, Path B). Output is byte-identical to C ZSTD_compressStream2 after ZSTD_CCtx_loadDictionary (e.g. zstd::stream::write::Encoder::with_dictionary) for the same operations.

Current scope: the unknown-content-size (attach) path — the natural streaming case. A stream whose source grows past the window (so C would drop the attached dictionary) returns a clean Error::Encode; so does a pledged size above the strategy’s attach cutoff (the copy path) and a dictionary with <= 8 bytes of content. Raw and trained dictionaries are both supported.

Source

pub fn with_pledged_src_size(level: i32, size: u64) -> Self

ZSTD_CCtx_setPledgedSrcSize: declare the total content size up front. Compression parameters are then derived from it exactly as in the one-shot path, the frame header carries the content size, and the stream errors if the fed input does not match the pledge. Note that C overrides the pledge when the first operation is finish (the input of that call becomes the pledge); this port is faithful to that.

Source

pub fn with_checksum(self, on: bool) -> Self

Enable the content checksum (ZSTD_c_checksumFlag): XXH64 of the content, low 32 bits appended to the frame.

§Panics

If streaming has already started (the flag applies at init time).

Source

pub fn with_workers( self, nb_workers: u32, job_size: u64, overlap_log: i32, ) -> Self

ZSTD_c_nbWorkers (+ optional ZSTD_c_jobSize / ZSTD_c_overlapLog, 0 = C default): enable multithreaded streaming. C’s MT output is deterministic and worker-count-independent, so this reproduces it single-threaded (see crate::compress_mt).

Current scope: unknown-size streaming (the default) via compress + finish, with or without a content checksum (with_checksum). A first-call finish below ZSTDMT_JOBSIZE_MIN (512 KiB) produces the single-threaded frame, and above it the one-shot MT frame. flush, a pledged size, and a dictionary with workers are not supported yet (clean Error::Encode).

§Panics

If streaming has already started (workers apply at init time).

Source

pub fn compress(&mut self, input: &[u8], out: &mut Vec<u8>) -> Result<(), Error>

ZSTD_compressStream2(.., ZSTD_e_continue): consume input, appending any output produced to out. Input is buffered internally; output is only produced once full blocks are available.

Source

pub fn flush(&mut self, out: &mut Vec<u8>) -> Result<(), Error>

ZSTD_compressStream2(.., ZSTD_e_flush): compress whatever is buffered and emit it, ending the current block. The frame stays open.

Source

pub fn finish(self, input: &[u8], out: &mut Vec<u8>) -> Result<(), Error>

ZSTD_compressStream2(.., ZSTD_e_end): consume input (pass b"" for none), then end the frame — last block, optional checksum.

When this is the first operation on the encoder, C auto-pledges the content size from this call’s input, making the result byte-identical to the one-shot ZSTD_compress2; this port does the same.

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.