Skip to main content

CompressionCodec

Trait CompressionCodec 

Source
pub trait CompressionCodec: Send + Sync {
    // Required methods
    fn id(&self) -> CodecId;
    fn compress(&self, data: &[u8]) -> Result<Vec<u8>>;
    fn decompress(
        &self,
        data: &[u8],
        uncompressed_size: usize,
    ) -> Result<Vec<u8>>;
}
Expand description

A compression codec that can compress and decompress block data.

Implementations must be Send + Sync so they can be shared across threads and async tasks.

§Examples

Any built-in codec round-trips block bytes:

use array_format::{CompressionCodec, Lz4Codec};

let codec = Lz4Codec;
let data = b"some block bytes";
let compressed = codec.compress(data)?;
let restored = codec.decompress(&compressed, data.len())?;
assert_eq!(restored, data);

§Extensibility

Implement this trait to add support for custom compression algorithms (e.g. zstd, lz4, snappy). Register the codec by its CodecId::Named identifier.

Required Methods§

Source

fn id(&self) -> CodecId

Returns the CodecId that identifies this codec in the footer.

Source

fn compress(&self, data: &[u8]) -> Result<Vec<u8>>

Compresses data and returns the compressed bytes.

Source

fn decompress(&self, data: &[u8], uncompressed_size: usize) -> Result<Vec<u8>>

Decompresses data and returns the original bytes.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§