kafka_protocol/
compression.rs

1//! Provides compression utilities for encoding records.
2//!
3//! This module has implementations of gzip, Snappy, as well as a noop compression format that
4//! allows encoding and decoding records into a [`Record`](crate::records::Record).
5
6use crate::protocol::buf::{ByteBuf, ByteBufMut};
7use anyhow::Result;
8
9#[cfg(feature = "gzip")]
10mod gzip;
11#[cfg(feature = "lz4")]
12mod lz4;
13mod none;
14#[cfg(feature = "snappy")]
15mod snappy;
16#[cfg(feature = "zstd")]
17mod zstd;
18
19#[cfg(feature = "gzip")]
20pub use gzip::Gzip;
21#[cfg(feature = "lz4")]
22pub use lz4::Lz4;
23pub use none::None;
24#[cfg(feature = "snappy")]
25pub use snappy::Snappy;
26#[cfg(feature = "zstd")]
27pub use zstd::Zstd;
28
29/// A trait for record compression algorithms.
30pub trait Compressor<B: ByteBufMut> {
31    /// Target buffer type for compression.
32    type BufMut: ByteBufMut;
33    /// Compresses into provided [`ByteBufMut`], with records encoded by `F` into `R`.
34    fn compress<R, F>(buf: &mut B, f: F) -> Result<R>
35    where
36        F: FnOnce(&mut Self::BufMut) -> Result<R>;
37}
38
39/// A trait for record decompression algorithms.
40pub trait Decompressor<B: ByteBuf> {
41    /// Target buffer type for decompression.
42    type Buf: ByteBuf;
43    /// Decompress records from `B` mapped using `F` into `R`.
44    fn decompress<R, F>(buf: &mut B, f: F) -> Result<R>
45    where
46        F: FnOnce(&mut Self::Buf) -> Result<R>;
47}