pub struct CompressContext { /* private fields */ }Expand description
Reusable compression context that amortizes hash table and buffer allocations.
Holds internal state (hash tables, output buffer, block encoder workspace) across calls. Useful when compressing many small inputs in a loop.
let mut ctx = zrip::CompressContext::new(1).unwrap();
for i in 0..10 {
let data = format!("message {i}").repeat(100);
let compressed = ctx.compress(data.as_bytes()).unwrap();
assert!(compressed.len() < data.len());
}Implementations§
Source§impl CompressContext
impl CompressContext
Sourcepub fn new(level: i32) -> Result<CompressContext, CompressError>
pub fn new(level: i32) -> Result<CompressContext, CompressError>
Creates a new context for the given compression level (-7..=4; -8 runs as -7).
It allocates nothing up front: the first frame sizes the hash tables and buffers for its input.
Sourcepub fn with_dict(
level: i32,
dict: Dictionary,
) -> Result<CompressContext, CompressError>
pub fn with_dict( level: i32, dict: Dictionary, ) -> Result<CompressContext, CompressError>
Creates a new context with a pre-loaded dictionary.
The prepared hash table snapshot is for the large-input parameter tier, built by the first input that uses it. Inputs whose tiered params match its hash sizes use the fast snapshot-restore path. Inputs up to 8 KiB, and some other one-block inputs, search the dictionary’s own tables instead; the rest fall back to per-call prefix hashing.
Use with_dict_for_size to build the
snapshot for a specific input size tier.
Sourcepub fn with_dict_for_size(
level: i32,
dict: Dictionary,
expected_size: usize,
) -> Result<CompressContext, CompressError>
pub fn with_dict_for_size( level: i32, dict: Dictionary, expected_size: usize, ) -> Result<CompressContext, CompressError>
Creates a new context with a pre-loaded dictionary, optimized for
inputs of approximately expected_size bytes.
The prepared hash table snapshot is for the parameter tier matching
expected_size, built by the first input that uses it. Inputs in
the same tier use the fast snapshot-restore path.
Sourcepub fn set_level(&mut self, level: i32) -> Result<(), CompressError>
pub fn set_level(&mut self, level: i32) -> Result<(), CompressError>
Switches later frames to level (-7..=4; -8 runs as -7).
Hash tables and buffers stay allocated and resize on use. A context with a dictionary prepares the dictionary again for the new level.
Sourcepub const fn set_content_checksum(&mut self, enabled: bool)
pub const fn set_content_checksum(&mut self, enabled: bool)
Enables or disables the standard Zstandard frame content checksum.
Checksums are enabled by default. Disabling them is useful when an enclosing format already authenticates the decoded content.
Sourcepub const fn content_checksum(&self) -> bool
pub const fn content_checksum(&self) -> bool
Whether newly compressed frames include a content checksum.
Sourcepub fn compress(&mut self, input: &[u8]) -> Result<Cow<'_, [u8]>, CompressError>
pub fn compress(&mut self, input: &[u8]) -> Result<Cow<'_, [u8]>, CompressError>
Compresses input using the context’s level and optional dictionary.
The frame borrows the context’s output buffer. A frame of 512 KiB or more takes the buffer instead, and the next call allocates a new one.
Sourcepub fn compress_into(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, CompressError>
pub fn compress_into( &mut self, input: &[u8], output: &mut [u8], ) -> Result<usize, CompressError>
Compresses input into a caller-owned buffer while reusing this
context’s hash tables, workspace, and dictionary.
Writes one frame to the start of output and returns its length.
Unlike Self::compress, the result can outlive the next use of this
context without copying. zrip::compress_bound(input.len()) bytes are
always enough. Returns CompressError::OutputTooSmall if the frame
does not fit; output then holds a partial frame.
Sourcepub fn compress_with_dict(
&mut self,
input: &[u8],
dict: &Dictionary,
) -> Result<Cow<'_, [u8]>, CompressError>
pub fn compress_with_dict( &mut self, input: &[u8], dict: &Dictionary, ) -> Result<Cow<'_, [u8]>, CompressError>
Compresses input using an ad-hoc dictionary (overrides the stored one).