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<Self, CompressError>
pub fn new(level: i32) -> Result<Self, CompressError>
Creates a new context for the given compression level (-7..=4).
Sourcepub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError>
pub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError>
Creates a new context with a pre-loaded dictionary.
The prepared hash table snapshot is built for the T0 (>256 KB) parameter tier. Inputs whose tiered params match T0’s hash sizes use the fast snapshot-restore path; others 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<Self, CompressError>
pub fn with_dict_for_size( level: i32, dict: Dictionary, expected_size: usize, ) -> Result<Self, CompressError>
Creates a new context with a pre-loaded dictionary, optimized for
inputs of approximately expected_size bytes.
The prepared hash table snapshot is built for the parameter tier
matching expected_size. Inputs in the same tier use the fast
snapshot-restore path.
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.
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).