Expand description
Zstd envelope compression with automatic raw fallback.
An envelope is [type][payload]: the first byte is a Type, and the
payload is either the input unchanged or a magicless Zstandard frame.
encode skips inputs shorter than 7 bytes and known compressed formats
directly, and probes inputs of 128 KiB and up before a full compression
pass. Whenever the compressed frame is not smaller than the input, raw bytes
are stored instead.
Encoder and decoder contexts are reused per thread across calls. Use
decode_limit when decoding untrusted inputs to bound the decompressed
size.
encode_owned keeps the input allocation for raw payloads and returns the
tag separately as Encoded, while encode_into writes into a
caller-provided buffer. decode_owned shifts a raw payload over its tag
without allocating.
The optional adaptive feature adds Adaptive, which folds measured
compression speed and ratio into a network-aware level choice.
use autozstd::{Level, decode, encode};
let data = b"hello world ".repeat(100);
let packed = encode(&data, Level::Balanced);
assert_eq!(decode(&packed)?, data);Structs§
- Adaptive
adaptive - Network-aware level chooser that learns from previous calls.
- Encoded
- Encoded payload with the type tag kept separate from the payload buffer.
- Stat
adaptive - Estimated behavior of one level.
Enums§
- Choice
adaptive - Chosen compression mode.
- Error
- Errors returned when decoding an envelope.
- Level
- Compression level presets.
- Type
- Payload kind, stored in the first byte of an envelope.
Functions§
- decode
- Decodes an envelope produced by
encode. - decode_
limit - Decodes an envelope produced by
encode, rejecting any payload whose decoded size exceedsmax_lenbytes. - decode_
owned - Decodes an owned envelope, reusing its buffer for raw payloads.
- decode_
owned_ limit - Decodes an owned envelope, rejecting any payload whose decoded size exceeds
max_lenbytes. - encode
- Compresses
datainto an envelope. - encode_
into - Encodes into
out, reusing its capacity, and returns the payload kind. - encode_
owned - Encodes an owned buffer, keeping it unchanged when raw bytes are chosen.