1mod buffer;
7#[cfg(feature = "compression")]
8pub(crate) mod compression;
9mod decode;
10mod encode;
11#[cfg(feature = "prost")]
12mod prost;
13
14use crate::Status;
15use std::io;
16
17pub(crate) use self::encode::{encode_client, encode_server};
18
19pub use self::buffer::{DecodeBuf, EncodeBuf};
20#[cfg(feature = "compression")]
21#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
22pub use self::compression::{CompressionEncoding, EnabledCompressionEncodings};
23pub use self::decode::Streaming;
24#[cfg(feature = "prost")]
25#[cfg_attr(docsrs, doc(cfg(feature = "prost")))]
26pub use self::prost::ProstCodec;
27
28const HEADER_SIZE: usize =
30 std::mem::size_of::<u8>() +
32 std::mem::size_of::<u32>();
34
35pub trait Codec: Default {
37 type Encode: Send + 'static;
39 type Decode: Send + 'static;
41
42 type Encoder: Encoder<Item = Self::Encode, Error = Status> + Send + 'static;
44 type Decoder: Decoder<Item = Self::Decode, Error = Status> + Send + 'static;
46
47 fn encoder(&mut self) -> Self::Encoder;
49 fn decoder(&mut self) -> Self::Decoder;
51}
52
53pub trait Encoder {
55 type Item;
57
58 type Error: From<io::Error>;
62
63 fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error>;
65}
66
67pub trait Decoder {
69 type Item;
71
72 type Error: From<io::Error>;
74
75 fn decode(&mut self, src: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error>;
81}