async_codec_lite/codec/
mod.rs

1use ::bytes::BytesMut;
2
3macro_rules! impl_phantom {
4    ($t:ident < $($param:ident),+ >) => {
5        impl<$($param),+> $t<$($param),+> {
6            #[allow(missing_docs)]
7            pub const fn new() -> Self {
8                Self(PhantomData)
9            }
10        }
11        impl<$($param),+> ::std::clone::Clone for $t<$($param),+> {
12            fn clone(&self) -> Self { Self::new() }
13        }
14        impl<$($param),+> ::std::fmt::Debug for $t<$($param),+> {
15            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
16                f.debug_struct(stringify!($t)).finish()
17            }
18        }
19        impl<$($param),+> ::std::default::Default for $t<$($param),+> {
20            fn default() -> Self { Self::new() }
21        }
22        impl<$($param),+> ::std::cmp::PartialEq for $t<$($param),+> {
23            fn eq(&self, _other: &Self) -> bool { true }
24        }
25    }
26}
27
28mod bytes;
29pub use self::bytes::BytesCodec;
30
31mod length;
32pub use self::length::{LengthCodec, OverflowError};
33
34mod limit;
35pub use self::limit::{DecoderWithSkipAhead, LimitCodec, LimitError, SkipAheadHandler};
36
37#[cfg(feature = "lines")]
38mod lines;
39#[cfg(feature = "lines")]
40pub use self::lines::LinesCodec;
41
42#[cfg(feature = "cbor")]
43mod cbor;
44#[cfg(feature = "cbor")]
45pub use self::cbor::CborCodec;
46
47#[cfg(feature = "json")]
48mod json;
49#[cfg(feature = "json")]
50pub use self::json::JsonCodec;
51
52pub trait Decoder {
53    type Item;
54    type Error: std::error::Error + 'static;
55
56    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>;
57
58    fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
59        self.decode(src)
60    }
61}
62
63pub trait Encoder {
64    type Item;
65    type Error: std::error::Error + 'static;
66
67    fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error>;
68}