pub fn compress_to_vec<R: Read>(source: R, level: CompressionLevel) -> Vec<u8> ⓘExpand description
Convenience function to compress some source into a Vec without reusing any resources of the compressor.
This helper eagerly buffers the full input (Read) before compression so it
can provide a source-size hint to the one-shot encoder path. Peak memory can
therefore be roughly input_size + output_size. For very large payloads or
tighter memory budgets, prefer streaming APIs such as StreamingEncoder.
This is NOT a streaming API. The source is fully buffered
into a Vec<u8> before any compression work begins, so peak input
memory is bounded by source.len() (not “constant regardless of
payload size” as a stream-shaped encoder would offer). If the
source is large enough that holding it in memory is not acceptable,
use StreamingEncoder which consumes chunks incrementally
without the up-front Vec build.
This helper drives read_to_end to materialize the full source
into a Vec<u8> before forwarding the slice to
compress_slice_to_vec. For a Read whose size is unknown ahead
of time, read_to_end grows that input Vec via power-of-two
doubling: peak input allocation can be up to 2× the final source
length transiently. The live working set on this entry point is
roughly input.capacity() plus the block-accumulation buffer and
per-block scratch carried by compress_slice_to_vec, plus the
exactly-sized output Vec. StreamingEncoder avoids the input
materialization step entirely and is the right entry point when
the source is large or unbounded.
use structured_zstd::encoding::{compress_to_vec, CompressionLevel};
let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
let compressed = compress_to_vec(data, CompressionLevel::Fastest);