Skip to main content

CompressContext

Struct CompressContext 

Source
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

Source

pub fn new(level: i32) -> Result<Self, CompressError>

Creates a new context for the given compression level (-7..=4).

Source

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.

Source

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.

Source

pub fn compress(&mut self, input: &[u8]) -> Result<Cow<'_, [u8]>, CompressError>

Compresses input using the context’s level and optional dictionary.

Source

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

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.