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
impl StreamEncoder
Sourcepub fn new(level: i32) -> Self
pub fn new(level: i32) -> Self
A streaming encoder with unknown content size (the
ZSTD_compressStream2 default).
Sourcepub fn with_dictionary(level: i32, dict: &[u8]) -> Self
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.
Sourcepub fn with_pledged_src_size(level: i32, size: u64) -> Self
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.
Sourcepub fn with_checksum(self, on: bool) -> Self
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).
Sourcepub fn with_workers(
self,
nb_workers: u32,
job_size: u64,
overlap_log: i32,
) -> Self
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).
Sourcepub fn compress(&mut self, input: &[u8], out: &mut Vec<u8>) -> Result<(), Error>
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.
Sourcepub fn flush(&mut self, out: &mut Vec<u8>) -> Result<(), Error>
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.
Sourcepub fn finish(self, input: &[u8], out: &mut Vec<u8>) -> Result<(), Error>
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.