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; -8 runs as -7).

It allocates nothing up front: the first frame sizes the hash tables and buffers for its input.

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

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

Source

pub const fn level(&self) -> i32

The level of the next frame.

Source

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.

Source

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.

Source

pub const fn content_checksum(&self) -> bool

Whether newly compressed frames include a content checksum.

Source

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.

Source

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.

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, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.