use std::io;
use dyn_clone::DynClone;
use super::Frame;
mod chain;
mod compression;
mod encryption;
mod plain;
mod predicate;
pub use chain::*;
pub use compression::*;
pub use encryption::*;
pub use plain::*;
pub use predicate::*;
/// Represents abstraction that implements specific encoder and decoder logic to transform an
/// arbitrary collection of bytes. This can be used to encrypt and authenticate bytes sent and
/// received by transports.
pub trait Codec: DynClone {
/// Encodes a frame's item
fn encode<'a>(&mut self, frame: Frame<'a>) -> io::Result>;
/// Decodes a frame's item
fn decode<'a>(&mut self, frame: Frame<'a>) -> io::Result>;
}
/// Represents a [`Box`]ed version of [`Codec`]
pub type BoxedCodec = Box;
macro_rules! impl_traits {
($($x:tt)+) => {
impl Clone for Box {
fn clone(&self) -> Self {
dyn_clone::clone_box(&**self)
}
}
impl Codec for Box {
fn encode<'a>(&mut self, frame: Frame<'a>) -> io::Result> {
Codec::encode(self.as_mut(), frame)
}
fn decode<'a>(&mut self, frame: Frame<'a>) -> io::Result> {
Codec::decode(self.as_mut(), frame)
}
}
};
}
impl_traits!(Codec);
impl_traits!(Codec + Send);
impl_traits!(Codec + Sync);
impl_traits!(Codec + Send + Sync);
/// Interface that provides extensions to the codec interface
pub trait CodecExt {
/// Chains this codec with another codec
fn chain(self, codec: T) -> ChainCodec
where
Self: Sized;
}
impl CodecExt for C {
fn chain(self, codec: T) -> ChainCodec {
ChainCodec::new(self, codec)
}
}