pub struct EncoderDictionary { /* private fields */ }Expand description
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).
Like libzstd’s ZSTD_dct_auto, data that starts with the dictionary
magic number (0xEC30A437) is a zstd-format dictionary (as made by
zstd --train: ID, entropy tables, repcodes, content), anything else
raw content. Dictionaries under 8 bytes are ignored (no ID, no
content).
use zstd_native::{Compressor, EncoderDictionary};
let dict = EncoderDictionary::new(b"common words, common phrases", 3)?;
let mut c = Compressor::with_prepared_dictionary(&dict);
let compressed = c.compress(b"common phrases, common words")?;
let mut d = zstd_native::Decompressor::with_dictionary(zstd_native::Dictionary::new(
b"common words, common phrases",
)?);
assert_eq!(d.decompress(&compressed)?, b"common phrases, common words");Implementations§
Source§impl EncoderDictionary
impl EncoderDictionary
Sourcepub fn new(dict: &[u8], level: i32) -> Result<EncoderDictionary, Error>
pub fn new(dict: &[u8], level: i32) -> Result<EncoderDictionary, Error>
Prepares dict for compression at level (see
compress; 0 means the default, 3).
§Errors
Error::InvalidDictionary if a zstd-format dictionary is corrupt.