titan-api-codec 1.2.9

Helpers for encoding and decoding Titan API messages
Documentation
//! Defines common traits and implementations for transforms.

use bytes::Bytes;
use std::io;

/// Defines a transformation on a set of binary data.
///
/// This is specifically designed to operate on entire messages instead of streams,
/// in order to better match now WebSocket messages are handled.
///
/// Examples are compression and decompression.
pub trait BinaryTransform {
    /// Applies the transform to the data, returning the resulting transformed data.
    ///
    /// The caller retains ownership of the source buffer.
    fn transform(&self, data: Bytes) -> Result<Bytes, io::Error>;

    /// Applies the transform to the data while possibly mutating the transformer.
    ///
    /// The caller retains ownership of the source buffer.
    ///
    /// Default implementation simply forwards to `[transform()]`.
    fn transform_mut(&mut self, data: Bytes) -> Result<Bytes, io::Error> {
        self.transform(data)
    }
}

/// Transform that returns data un-modified.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoOpTransform;

impl BinaryTransform for NoOpTransform {
    #[inline]
    fn transform(&self, data: Bytes) -> Result<Bytes, io::Error> {
        Ok(data)
    }
}