use std::{fmt, io, rc::Rc};
use ntex_bytes::{BytePages, Bytes, BytesMut};
pub trait Encoder {
type Item;
type Error: fmt::Debug;
#[deprecated(since = "1.2.0", note = "Implement .encodev() method.")]
fn encode(&self, _: Self::Item, _: &mut BytesMut) -> Result<(), Self::Error> {
panic!("Encoder::encodev() must be implemented")
}
fn encodev(&self, item: Self::Item, dst: &mut BytePages) -> Result<(), Self::Error> {
let mut buf = BytesMut::new();
#[allow(deprecated)]
self.encode(item, &mut buf)?;
dst.append(buf.freeze());
Ok(())
}
}
pub trait Decoder {
type Item: fmt::Debug;
type Error: fmt::Debug;
fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>;
}
impl<T> Encoder for Rc<T>
where
T: Encoder,
{
type Item = T::Item;
type Error = T::Error;
#[allow(deprecated)]
fn encode(&self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
(**self).encode(item, dst)
}
fn encodev(&self, item: Self::Item, dst: &mut BytePages) -> Result<(), Self::Error> {
(**self).encodev(item, dst)
}
}
impl<T> Decoder for Rc<T>
where
T: Decoder,
{
type Item = T::Item;
type Error = T::Error;
fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
(**self).decode(src)
}
}
#[derive(Debug, Copy, Clone)]
pub struct BytesCodec;
impl Encoder for BytesCodec {
type Item = Bytes;
type Error = io::Error;
#[inline]
fn encodev(&self, item: Bytes, dst: &mut BytePages) -> Result<(), Self::Error> {
dst.append(item);
Ok(())
}
}
impl Decoder for BytesCodec {
type Item = Bytes;
type Error = io::Error;
fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.is_empty() {
Ok(None)
} else {
Ok(Some(src.split_to(src.len())))
}
}
}