Skip to main content

Module encoding

Module encoding 

Source
Expand description

Zstandard encoder — frame compression, streaming, dictionary support.

Four entry points cover the common use cases:

  • compress — one-shot helper that builds a self-contained Zstandard frame from a Read source to a Write sink. The input is consumed incrementally from Read, so input buffering stays bounded; however, the compressed output is buffered in memory until the frame is complete so the Frame Content Size field can be filled in the header — peak memory is O(compressed_size) (worst-case O(input_size) for incompressible payloads, plus a small frame overhead). The savings vs compress_to_vec come from not materialising the input alongside the output.
  • compress_to_vec — same one-shot path as compress but the input is eagerly drained into an internal Vec first (read_to_end) so the encoder can be handed a &[u8] and a precise source-size hint. Peak memory is therefore ≈ input_size + output_size; prefer compress or StreamingEncoder when the input is large or unbounded.
  • StreamingEncoder — implements crate::io::Write, which re-exports std::io::Write under the std feature and falls back to a no_std-friendly trait otherwise. Accepts bytes incrementally and flushes compressed output as blocks fill. Requires set_pledged_content_size before the first write if the Frame Content Size field is to be populated.
  • FrameCompressor — lower-level builder that owns the matcher and the per-frame configuration; the streaming and one-shot helpers are thin wrappers over it. Reach for it when you need to swap in a custom Matcher implementation or share the matcher across frames.

Compression intensity is selected via CompressionLevel, which provides both named presets (Fastest, Default, Better, Best) and numeric levels (from_level(n)) that mirror C zstd’s level numbering (negative for ultra-fast, 0 = default, 1..=22 for the standard range).

All produced frames are valid RFC 8878 Zstandard streams and decode through both this crate’s crate::decoding module and upstream C zstd.

Re-exports§

pub use frame_emit_info::BlockType;lsm
pub use frame_emit_info::FrameBlock;lsm
pub use frame_emit_info::FrameEmitInfo;lsm

Modules§

frame_emit_infolsm
Structural metadata describing the layout of an emitted zstd frame.

Structs§

FrameCompressor
An interface for compressing arbitrary data with the ZStandard compression algorithm.
MatchGeneratorDriver
This is the default implementation of the Matcher trait. It allocates and reuses the buffers when possible.
StreamingEncoder
Incremental frame encoder that implements Write.

Enums§

CompressionLevel
The compression mode used impacts the speed of compression, and resulting compression ratios. Faster compression will result in worse compression ratios, and vice versa.
Sequence
Sequences that a Matcher can produce

Traits§

Matcher
Trait used by the encoder that users can use to extend the matching facilities with their own algorithm making their own tradeoffs between runtime, memory usage and compression ratio

Functions§

compress
Convenience function to compress some source into a target without reusing any resources of the compressor
compress_slice_to_vec
Compress a contiguous byte slice into a fresh Vec<u8> without the input-buffering step that compress_to_vec performs to adapt a Read source. Donor-parity peak-memory shape: the input is read by reference, and the output Vec is seeded with min(compress_bound(src), OUTPUT_BLOCK_CAP = 128 KiB).
compress_to_vec
Convenience function to compress some source into a Vec without reusing any resources of the compressor.