Skip to main content

Crate zstd_native

Crate zstd_native 

Source
Expand description

Pure Rust Zstandard (zstd, RFC 8878) decompression and compression, built to match or beat C libzstd 1.5.7 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.5.7 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§

CompressProgress
What one StreamEncoder::compress call 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.
Decoderstd
Decompresses a zstd stream from a reader (std::io::Read), frame after frame, like the zstd crate’s Decoder.
Decompressor
A reusable decompressor (libzstd’s ZSTD_DCtx).
Dictionary
A decompression dictionary, parsed once and reused for many frames (libzstd’s ZSTD_DDict).
Encoderstd
Compresses into a writer (std::io::Write), like the zstd crate’s Encoder: one zstd frame, ended by finish. The output is byte-identical to libzstd 1.5.7’s streaming compressor’s (ZSTD_compressStream2) for the same level, writes and flushes.
EncoderDictionary
A dictionary prepared for compression at a level (libzstd’s ZSTD_CDict, ZSTD_createCDict): parsed and indexed once, then used by any number of Compressors, StreamEncoders and Encoders (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, the zstd crate’s with_prepared_dictionary).
FrameHeader
A parsed zstd frame header.
Progress
What one StreamDecoder::decode call did.
StreamDecoder
A streaming decoder: feed it input in any pieces, get output in any pieces. Handles concatenated and skippable frames. No std needed; Decoder wraps it as a std::io::Read.
StreamEncoder
A streaming compressor (libzstd’s ZSTD_CStream with ZSTD_compressStream2): feed it input in any pieces, get output in any pieces, frame after frame. The output is byte-identical to libzstd 1.5.7’s for the same level, input, operations and output buffer sizes (see compress). No std needed; Encoder wraps it as a std::io::Write.

Enums§

EndOp
What StreamEncoder::compress does after taking the input (libzstd’s ZSTD_EndDirective).
Error
Errors returned by the decoder and the encoder.

Functions§

compress
Compresses src into one zstd frame at level (1 to 22, negative for faster; 0 means the default, 3), like libzstd’s ZSTD_compress: the output is byte-identical to libzstd 1.5.7’s.
compress_bound
libzstd’s ZSTD_COMPRESSBOUND: the largest compressed size of n bytes.
compress_to_slice
Compresses into dst, returning the compressed size; dst of compress_bound(src.len()) bytes is always enough.
compress_with_dictionary
Compresses src into one zstd frame at level with the dictionary dict (zstd-format or raw content, see EncoderDictionary; an empty one is no dictionary). The output is byte-identical to libzstd 1.5.7’s ZSTD_CCtx_loadDictionary then ZSTD_compress2 on a new context (the zstd crate’s bulk::Compressor::with_dictionary(level, dict)). It indexes the dictionary on every call: for many inputs, use a Compressor (which indexes it once) or an EncoderDictionary.
content_size
The decompressed size stored in the header of the first frame, or None when the header doesn’t store it.
decompress
Decompresses every frame in src into a new vector.
decompress_to_slice
Decompresses every frame in src into dst, returning the size.
xxh64
XXH64 of data with seed 0.