Skip to main content

Compressor

Struct Compressor 

Source
pub struct Compressor { /* private fields */ }
Expand description

A reusable compressor (libzstd’s ZSTD_CCtx): keeps its tables and buffers between frames, which matters for many small inputs, and optionally a dictionary.

let mut c = zstd_native::Compressor::new(1);
let a = c.compress(b"first message, first message")?;
let b = c.compress(b"second message")?;
assert_eq!(zstd_native::decompress(&a)?, b"first message, first message");
assert_eq!(zstd_native::decompress(&b)?, b"second message");

With a dictionary, its frames are byte-identical to those of libzstd’s context with the dictionary loaded (ZSTD_CCtx_loadDictionary, the zstd crate’s bulk::Compressor::with_dictionary) or referenced (ZSTD_CCtx_refCDict, with_prepared_dictionary):

let dict = b"{\"id\": 0, \"name\": \"\", \"tags\": []}";
let mut c = zstd_native::Compressor::with_dictionary(3, dict)?;
let a = c.compress(b"{\"id\": 1, \"name\": \"one\", \"tags\": [\"a\"]}")?;
let mut d = zstd_native::Decompressor::with_dictionary(zstd_native::Dictionary::new(dict)?);
assert_eq!(d.decompress(&a)?, b"{\"id\": 1, \"name\": \"one\", \"tags\": [\"a\"]}");

With std, it can compress with worker threads (set_workers), as libzstd’s ZSTD_compress2 with ZSTD_c_nbWorkers.

Implementations§

Source§

impl Compressor

Source

pub fn new(level: i32) -> Self

A compressor for level (see compress).

Source

pub fn set_checksum(&mut self, on: bool)

Ends each frame with a content checksum (libzstd’s ZSTD_c_checksumFlag; off by default): the low 32 bits of the XXH64 of the frame’s content, which decoders verify. It costs 4 bytes per frame and the time to hash the input.

let mut c = zstd_native::Compressor::new(3);
c.set_checksum(true);
let a = c.compress(b"checked, checked, checked")?;
assert_eq!(zstd_native::decompress(&a)?, b"checked, checked, checked");
Source

pub fn set_content_size_flag(&mut self, on: bool)

Writes the content size into the frame header (libzstd’s ZSTD_c_contentSizeFlag; on by default). Off, the header leaves it out (decoders then cannot tell the size before decoding); the compressed blocks are the same.

Source

pub fn set_dict_id_flag(&mut self, on: bool)

Writes the dictionary’s ID into the frame header, when a dictionary with an ID is used (libzstd’s ZSTD_c_dictIDFlag; on by default). Off, the header leaves it out (decoders then cannot check that they have the right dictionary); the compressed blocks are the same.

Source

pub fn set_workers(&mut self, n: u32)

Available on crate feature std only.

Compresses with n worker threads (libzstd’s ZSTD_c_nbWorkers; at most 256, 64 on 32-bit targets); 0, the default, compresses in the calling thread. The output is byte-identical to libzstd’s ZSTD_compress2 with the same worker count (a different count changes it, see below).

As in libzstd, inputs of at most 512 KiB are still compressed in the calling thread; larger ones are cut into jobs (by default 4 << windowLog bytes, at least 1 MiB: 8 MiB at level 3, 32 MiB at level 19), each compressed by a worker starting from the end of the previous one, so a frame needs several jobs to use several workers. Multithreaded output differs from single-threaded output, and compresses slightly less well.

The threads are started at the first frame that uses them and kept until the compressor is dropped (or the count changes). Memory: per worker, a compressor’s tables for the level; per job in flight (at most the smallest power of 2 above the worker count plus 2), its input and output.

On 32-bit targets, large jobs and many workers can need more memory than the address space has: libzstd’s round buffer of the input is workers + 3 jobs (+ 2 without overlap; at least the window plus the slack jobs with long distance matching), so level 22’s 512 MiB jobs, or a 512 MiB job size, need 2 GiB with one worker. Compressing a frame then fails with Error::MemoryAllocation, as libzstd fails to allocate it (memory_allocation).

Source

pub fn workers(&self) -> u32

Available on crate feature std only.

The worker thread count (see set_workers).

Source

pub fn with_dictionary(level: i32, dict: &[u8]) -> Result<Self, Error>

A compressor for level with the dictionary dict (see set_dictionary).

§Errors

Error::InvalidDictionary if a zstd-format dictionary is corrupt.

Source

pub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self

A compressor with a prepared dictionary, at its level (see set_prepared_dictionary).

Source

pub fn set_level(&mut self, level: i32)

Changes the level for the next frames. With a prepared dictionary, frames take the dictionary’s level instead; a dictionary given to set_dictionary is indexed at the level of the first frame that uses it (as in libzstd).

Source

pub fn set_dictionary(&mut self, dict: &[u8]) -> Result<(), Error>

Uses dict for the next frames (libzstd’s ZSTD_CCtx_loadDictionary), replacing any dictionary; an empty dict removes it. Zstd-format dictionaries (starting with the magic number 0xEC30A437) and raw content are both accepted, see EncoderDictionary. The dictionary is indexed for the level of the next frame, then kept.

§Errors

Error::InvalidDictionary if a zstd-format dictionary is corrupt; the compressor then has no dictionary.

Source

pub fn set_prepared_dictionary(&mut self, dict: &EncoderDictionary)

Uses the prepared dict for the next frames, at its level (libzstd’s ZSTD_CCtx_refCDict), replacing any dictionary.

Source

pub fn clear_dictionary(&mut self)

Removes the dictionary.

Source

pub fn compress(&mut self, src: &[u8]) -> Result<Vec<u8>, Error>

Compresses src as one frame (see compress).

§Errors

As compress.

Source

pub fn compress_to_slice( &mut self, src: &[u8], dst: &mut [u8], ) -> Result<usize, Error>

Compresses src as one frame into dst (see compress_to_slice).

§Errors

As compress_to_slice.

Trait Implementations§

Source§

impl Debug for Compressor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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 = !

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.