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
use bytes::BytesMut;
use std::io;
use tokio_util::codec::{Decoder, Encoder};
pub trait Codec:
for<'a> Encoder<&'a [u8], Error = io::Error> + Decoder<Item = Vec<u8>, Error = io::Error> + Clone
{
fn encode(&mut self, item: &[u8], dst: &mut BytesMut) -> io::Result<()>;
fn decode(&mut self, src: &mut BytesMut) -> io::Result<Option<Vec<u8>>>;
}
macro_rules! impl_traits_for_codec {
($type:ident) => {
impl<'a> tokio_util::codec::Encoder<&'a [u8]> for $type {
type Error = io::Error;
fn encode(&mut self, item: &'a [u8], dst: &mut BytesMut) -> Result<(), Self::Error> {
Codec::encode(self, item, dst)
}
}
impl tokio_util::codec::Decoder for $type {
type Item = Vec<u8>;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
Codec::decode(self, src)
}
}
};
}
mod plain;
pub use plain::PlainCodec;
mod xchacha20poly1305;
pub use xchacha20poly1305::XChaCha20Poly1305Codec;