zstd/bulk/mod.rs
1//! Compress and decompress data in bulk.
2//!
3//! These methods process all the input data at once.
4//! It is therefore best used with relatively small blocks
5//! (like small network packets).
6
7mod compressor;
8mod decompressor;
9
10#[cfg(test)]
11mod tests;
12
13pub use self::compressor::Compressor;
14pub use self::decompressor::Decompressor;
15
16use std::io;
17
18/// Compresses a single block of data to the given destination buffer.
19///
20/// Returns the number of bytes written, or an error if something happened
21/// (for instance if the destination buffer was too small).
22///
23/// A level of `0` uses zstd's default (currently `3`).
24pub fn compress_to_buffer(
25 source: &[u8],
26 destination: &mut [u8],
27 level: i32,
28) -> io::Result<usize> {
29 Compressor::new(level)?.compress_to_buffer(source, destination)
30}
31
32/// Compresses a block of data and returns the compressed result.
33///
34/// A level of `0` uses zstd's default (currently `3`).
35pub fn compress(data: &[u8], level: i32) -> io::Result<Vec<u8>> {
36 Compressor::new(level)?.compress(data)
37}
38
39/// Deompress a single block of data to the given destination buffer.
40///
41/// Returns the number of bytes written, or an error if something happened
42/// (for instance if the destination buffer was too small).
43pub fn decompress_to_buffer(
44 source: &[u8],
45 destination: &mut [u8],
46) -> io::Result<usize> {
47 Decompressor::new()?.decompress_to_buffer(source, destination)
48}
49
50/// Decompresses a block of data and returns the decompressed result.
51///
52/// The decompressed data should be less than `capacity` bytes,
53/// or an error will be returned.
54pub fn decompress(data: &[u8], capacity: usize) -> io::Result<Vec<u8>> {
55 Decompressor::new()?.decompress(data, capacity)
56}