Skip to main content

compress_slice_to_vec

Function compress_slice_to_vec 

Source
pub fn compress_slice_to_vec(source: &[u8], level: CompressionLevel) -> Vec<u8> 
Expand description

Compress a contiguous byte slice into a fresh Vec<u8> without the input-buffering step that compress_to_vec performs to adapt a Read source.

One-shot wrapper over FrameCompressor::compress_independent_frame: the input is read by reference (the eligible Fast path scans it in place, no per-block history copy), and the returned Vec is allocated exactly once at the final frame size after compression. Peak transient memory is the block-accumulation buffer (grown via amortized doubling, ≈ 2× current compressed size at the last realloc) plus the exactly-sized output. The worst-case compressed-size bound is never pinned upfront, so a highly compressible 100 MiB input does not charge ~100 MiB of worst-case expansion against peak.

To compress many slices, construct one FrameCompressor and call compress_independent_frame_into in a loop instead, which reuses the matcher tables, scratch, and output buffer across frames (this function allocates and primes from scratch each call).

§Panics

Panics on encoder error (matches the failure surface of compress_to_vec, which this function backs). Out-of-memory during the output / per-block scratch allocations is handled by the global allocator’s abort policy. The slice/Vec entry points mirror the donor ZSTD_compress shape (no error return on the bulk path).

use structured_zstd::encoding::{compress_slice_to_vec, CompressionLevel};
let data: &[u8] = &[0,0,0,0,0,0,0,0,0,0,0,0];
let compressed = compress_slice_to_vec(data, CompressionLevel::Fastest);