Skip to main content

ntex_codec/
lib.rs

1//! Utilities for encoding and decoding frames.
2
3use std::{fmt, io, rc::Rc};
4
5use ntex_bytes::{BytePages, Bytes, BytesMut};
6
7/// Trait of helper objects to write out messages as bytes.
8pub trait Encoder {
9    /// The type of items consumed by the `Encoder`
10    type Item;
11
12    /// The type of encoding errors.
13    type Error: fmt::Debug;
14
15    #[deprecated(since = "1.2.0", note = "Implement .encodev() method.")]
16    /// Encodes a frame into the buffer provided.
17    fn encode(&self, _: Self::Item, _: &mut BytesMut) -> Result<(), Self::Error> {
18        panic!("Encoder::encodev() must be implemented")
19    }
20
21    /// Encodes a frame into the buffer provided.
22    fn encodev(&self, item: Self::Item, dst: &mut BytePages) -> Result<(), Self::Error> {
23        let mut buf = BytesMut::new();
24        #[allow(deprecated)]
25        self.encode(item, &mut buf)?;
26        dst.append(buf.freeze());
27        Ok(())
28    }
29}
30
31/// Decoding of frames via buffers.
32pub trait Decoder {
33    /// The type of decoded frames.
34    type Item: fmt::Debug;
35
36    /// The type of unrecoverable frame decoding errors.
37    ///
38    /// If an individual message is ill-formed but can be ignored without
39    /// interfering with the processing of future messages, it may be more
40    /// useful to report the failure as an `Item`.
41    type Error: fmt::Debug;
42
43    /// Attempts to decode a frame from the provided buffer of bytes.
44    fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>;
45}
46
47impl<T> Encoder for Rc<T>
48where
49    T: Encoder,
50{
51    type Item = T::Item;
52    type Error = T::Error;
53
54    #[allow(deprecated)]
55    fn encode(&self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
56        (**self).encode(item, dst)
57    }
58
59    fn encodev(&self, item: Self::Item, dst: &mut BytePages) -> Result<(), Self::Error> {
60        (**self).encodev(item, dst)
61    }
62}
63
64impl<T> Decoder for Rc<T>
65where
66    T: Decoder,
67{
68    type Item = T::Item;
69    type Error = T::Error;
70
71    fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
72        (**self).decode(src)
73    }
74}
75
76/// Bytes codec.
77///
78/// Reads/Writes chunks of bytes from a stream.
79#[derive(Debug, Copy, Clone)]
80pub struct BytesCodec;
81
82impl Encoder for BytesCodec {
83    type Item = Bytes;
84    type Error = io::Error;
85
86    #[inline]
87    fn encodev(&self, item: Bytes, dst: &mut BytePages) -> Result<(), Self::Error> {
88        dst.append(item);
89        Ok(())
90    }
91}
92
93impl Decoder for BytesCodec {
94    type Item = Bytes;
95    type Error = io::Error;
96
97    fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
98        if src.is_empty() {
99            Ok(None)
100        } else {
101            Ok(Some(src.split_to(src.len())))
102        }
103    }
104}