1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use super::Frame;
use dyn_clone::DynClone;
use std::io;
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::*;
pub trait Codec: DynClone {
fn encode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>>;
fn decode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>>;
}
pub type BoxedCodec = Box<dyn Codec + Send + Sync>;
macro_rules! impl_traits {
($($x:tt)+) => {
impl Clone for Box<dyn $($x)+> {
fn clone(&self) -> Self {
dyn_clone::clone_box(&**self)
}
}
impl Codec for Box<dyn $($x)+> {
fn encode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>> {
Codec::encode(self.as_mut(), frame)
}
fn decode<'a>(&mut self, frame: Frame<'a>) -> io::Result<Frame<'a>> {
Codec::decode(self.as_mut(), frame)
}
}
};
}
impl_traits!(Codec);
impl_traits!(Codec + Send);
impl_traits!(Codec + Sync);
impl_traits!(Codec + Send + Sync);
pub trait CodecExt {
fn chain<T>(self, codec: T) -> ChainCodec<Self, T>
where
Self: Sized;
}
impl<C: Codec> CodecExt for C {
fn chain<T>(self, codec: T) -> ChainCodec<Self, T> {
ChainCodec::new(self, codec)
}
}