pub trait Codec: Send + Sync {
// Required methods
fn id(&self) -> u8;
fn name(&self) -> &'static str;
fn compress(&self, plaintext: &[u8]) -> Result<Vec<u8>, CoreError>;
fn decompress(
&self,
compressed: &[u8],
expected_len: u32,
) -> Result<Vec<u8>, CoreError>;
// Provided methods
fn min_compress_size(&self) -> usize { ... }
fn compress_with_tunables(
&self,
plaintext: &[u8],
tunables: &CodecTunables,
) -> Result<Vec<u8>, CoreError> { ... }
}Expand description
The behaviour every compression codec implements. New codecs register
a Codec impl with CodecRegistry::register; the dispatch code
never changes.
Required Methods§
Sourcefn compress(&self, plaintext: &[u8]) -> Result<Vec<u8>, CoreError>
fn compress(&self, plaintext: &[u8]) -> Result<Vec<u8>, CoreError>
Compress plaintext into the codec’s wire format.
§Errors
Returns CoreError::UnsupportedFeature if the codec is
decode-only in pure Rust (currently only XZ), or
CoreError::Corrupt if the encoder fails.
Sourcefn decompress(
&self,
compressed: &[u8],
expected_len: u32,
) -> Result<Vec<u8>, CoreError>
fn decompress( &self, compressed: &[u8], expected_len: u32, ) -> Result<Vec<u8>, CoreError>
Decompress compressed, verifying the output length matches
expected_len exactly.
§Errors
Returns CoreError::Corrupt if decompression fails or the
result length does not match expected_len.
Provided Methods§
Sourcefn min_compress_size(&self) -> usize
fn min_compress_size(&self) -> usize
Minimum input size for this codec to be tried in the compression tournament. Chunks smaller than this skip the codec entirely. Defaults to 0 (no threshold). Override in codec impls that have significant per-call setup cost (context model initialization, grammar construction, etc.).
Sourcefn compress_with_tunables(
&self,
plaintext: &[u8],
tunables: &CodecTunables,
) -> Result<Vec<u8>, CoreError>
fn compress_with_tunables( &self, plaintext: &[u8], tunables: &CodecTunables, ) -> Result<Vec<u8>, CoreError>
Compress with a tunables hint. Codecs that have user-tunable
parameters (PPMd order/budget, Brotli quality, ZSTD level,
Bzip2 block size, …) override this; the default impl ignores
tunables and calls compress. Adding a tunable is therefore
backward-compatible — old callers keep working.
§Errors
Same as Codec::compress.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".