Skip to main content

titan_api_codec/transform/
common.rs

1//! Defines common traits and implementations for transforms.
2
3use bytes::Bytes;
4use std::io;
5
6/// Defines a transformation on a set of binary data.
7///
8/// This is specifically designed to operate on entire messages instead of streams,
9/// in order to better match now WebSocket messages are handled.
10///
11/// Examples are compression and decompression.
12pub trait BinaryTransform {
13    /// Applies the transform to the data, returning the resulting transformed data.
14    ///
15    /// The caller retains ownership of the source buffer.
16    fn transform(&self, data: Bytes) -> Result<Bytes, io::Error>;
17
18    /// Applies the transform to the data while possibly mutating the transformer.
19    ///
20    /// The caller retains ownership of the source buffer.
21    ///
22    /// Default implementation simply forwards to `[transform()]`.
23    fn transform_mut(&mut self, data: Bytes) -> Result<Bytes, io::Error> {
24        self.transform(data)
25    }
26}
27
28/// Transform that returns data un-modified.
29#[derive(Debug, Clone, Copy, Default)]
30pub struct NoOpTransform;
31
32impl BinaryTransform for NoOpTransform {
33    #[inline]
34    fn transform(&self, data: Bytes) -> Result<Bytes, io::Error> {
35        Ok(data)
36    }
37}