Expand description
Pure Rust Zstandard (zstd, RFC 8878) decompression and compression,
built to match or beat C libzstd 1.6.0 in speed. No C code, no_std +
alloc capable.
§One-shot
// A frame holding "hello" as a raw block.
let compressed = [0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x05, 0x29, 0x00, 0x00, b'h', b'e', b'l', b'l', b'o'];
assert_eq!(zstd_native::decompress(&compressed)?, b"hello");Decompressor keeps its tables and buffers between calls (and can
hold a Dictionary); decompress_to_slice writes into a buffer
you provide.
§Streaming
Decoder reads a zstd stream from any std::io::Read (frame after
frame); StreamDecoder is the push API underneath (input and output
in pieces of any size, no std needed).
use std::io::Read;
let mut text = String::new();
zstd_native::Decoder::new(compressed).read_to_string(&mut text)?;§Compression
compress (and Compressor, compress_to_slice) produce the
same bytes as libzstd 1.6.0 at every level. Encoder compresses into
any std::io::Write; StreamEncoder is the push API underneath
(libzstd’s ZSTD_compressStream2, byte-identical for the same calls).
All of them take dictionaries too (compress_with_dictionary,
Compressor::with_dictionary, or an EncoderDictionary prepared
once), with libzstd’s output. With std, Compressor,
StreamEncoder and Encoder also compress with worker threads
(set_workers, libzstd’s ZSTD_c_nbWorkers), with libzstd’s
multithreaded output. Their set_checksum ends frames with a content
checksum (ZSTD_c_checksumFlag, off by default).
use std::io::Write;
let mut enc = zstd_native::Encoder::new(Vec::new(), 3);
enc.write_all(b"hello, hello, hello")?;
let compressed = enc.finish()?;
assert_eq!(zstd_native::decompress(&compressed).unwrap(), b"hello, hello, hello");§Speed
The hot loops (4-stream Huffman decoding, sequence decoding and
execution) are compiled twice, generic and with BMI2, and the CPU picks
one at run time (with std; without it the compile-time target
features decide). The portable feature forces the generic code.
Structs§
- Compress
Progress - What one
StreamEncoder::compresscall did. - Compressor
- A reusable compressor (libzstd’s
ZSTD_CCtx): keeps its tables and buffers between frames, which matters for many small inputs, and optionally a dictionary. - Decoder
std - Decompresses a zstd stream from a reader (
std::io::Read), frame after frame, like thezstdcrate’sDecoder. - Decompressor
- A reusable decompressor (libzstd’s
ZSTD_DCtx). - Dictionary
- A decompression dictionary, parsed once and reused for many frames
(libzstd’s
ZSTD_DDict). - Encoder
std - Compresses into a writer (
std::io::Write), like thezstdcrate’sEncoder: one zstd frame, ended byfinish. The output is byte-identical to libzstd 1.6.0’s streaming compressor’s (ZSTD_compressStream2) for the same level, writes and flushes. - Encoder
Dictionary - A dictionary prepared for compression at a level (libzstd’s
ZSTD_CDict,ZSTD_createCDict): parsed and indexed once, then used by any number ofCompressors,StreamEncoders andEncoders (cloning it is cheap: the tables are shared). Frames compressed with it take its level, and are byte-identical to libzstd’s with the same CDict (ZSTD_CCtx_refCDict, thezstdcrate’swith_prepared_dictionary). - Frame
Header - A parsed zstd frame header.
- Progress
- What one
StreamDecoder::decodecall did. - Stream
Decoder - A streaming decoder: feed it input in any pieces, get output in any
pieces. Handles concatenated and skippable frames. No
stdneeded;Decoderwraps it as astd::io::Read. - Stream
Encoder - A streaming compressor (libzstd’s
ZSTD_CStreamwithZSTD_compressStream2): feed it input in any pieces, get output in any pieces, frame after frame. The output is byte-identical to libzstd 1.6.0’s for the same level, input, operations and output buffer sizes (seecompress). Nostdneeded;Encoderwraps it as astd::io::Write.
Enums§
- EndOp
- What
StreamEncoder::compressdoes after taking the input (libzstd’sZSTD_EndDirective). - Error
- Errors returned by the decoder and the encoder.
Functions§
- compress
- Compresses
srcinto one zstd frame atlevel(1 to 22, negative for faster; 0 means the default, 3), like libzstd’sZSTD_compress: the output is byte-identical to libzstd 1.6.0’s. - compress_
bound - libzstd’s
ZSTD_COMPRESSBOUND: the largest compressed size ofnbytes. - compress_
to_ slice - Compresses into
dst, returning the compressed size;dstofcompress_bound(src.len())bytes is always enough. - compress_
with_ dictionary - Compresses
srcinto one zstd frame atlevelwith the dictionarydict(zstd-format or raw content, seeEncoderDictionary; an empty one is no dictionary). The output is byte-identical to libzstd 1.6.0’sZSTD_CCtx_loadDictionarythenZSTD_compress2on a new context (thezstdcrate’sbulk::Compressor::with_dictionary(level, dict)). It indexes the dictionary on every call: for many inputs, use aCompressor(which indexes it once) or anEncoderDictionary. - content_
size - The decompressed size stored in the header of the first frame, or
Nonewhen the header doesn’t store it. - decompress
- Decompresses every frame in
srcinto a new vector. - decompress_
to_ slice - Decompresses every frame in
srcintodst, returning the size. - xxh64
- XXH64 of
datawith seed 0.