Skip to main content

Crate embark_codec

Crate embark_codec 

Source
Expand description

The compression layer of the embark toolkit: one interface over DEFLATE, LZ4, Snappy, Zstandard and LZMA1, keyed by the CodecId that embark-format writes into every entry header.

There are two ways in. compress and decompress take a CodecId and dispatch at runtime, which is what a decoder reading an untrusted entry needs. The unit structs — Store, Deflate, Lz4, Snappy, Zstd, Lzma — are the direct-call form for a caller that already knows its codec, and compress_best_in runs the auto family’s codec selection: compress with one AutoTier’s worth of candidates and keep the smallest output.

Every codec is pure Rust with no C dependency, so the whole crate builds for no_std targets with only alloc.

§Features

Each codec has its own feature and none are on by default, so a build links only the formats it names. enc compiles the compressing half and dec the decompressing half; a consumer that only reads embedded files needs dec plus the codecs its entries actually use.

parallel-encode is separate from all of those: it does not change what is compiled, only how fast enc runs, by letting the encoder use more than one thread. See parallel.

A codec that was not compiled in is not an error at the format level: decompress reports Error::UnknownCodec for it, and compress falls back to CodecId::Store so encoding never fails.

§Example

use embark_format::CodecId;

let data = b"embark embark embark embark".repeat(8);
let packed = embark_codec::compress(CodecId::Deflate, &data);
assert!(packed.len() < data.len());

let back = embark_codec::decompress(CodecId::Deflate, &packed, data.len())?;
assert_eq!(back, data);

Modules§

parallelenc
The build-time encoder’s thread budget.

Structs§

Deflatedeflate
The DEFLATE codec (via miniz_oxide).
Lz4lz4
The LZ4 codec (via lz4_flex).
Lzmalzma
The LZMA1 codec (self-implemented .lzma alone format).
Snappysnappy
The Snappy codec (self-implemented).
Store
The identity codec: stores bytes unchanged.
Zstdzstd
The Zstd codec: a self-written encoder, decoding via ruzstd.

Enums§

AutoTierenc
Which codecs a selection pass is allowed to try.

Traits§

Codec
Associates a built-in codec type with its on-binary CodecId.

Functions§

compressenc
Compress with id, falling back to CodecId::Store when that codec’s feature is off, so encoding never fails.
compress_bestenc
Compress input with every codec compiled into this build and return the smallest result.
compress_best_inenc
Compress input with each of tier’s candidates compiled into this build and return the smallest result.
decompressdec
Decompress with id, which must name a codec compiled into this build.