tokio_util/codec/
encoder.rs

1use bytes::BytesMut;
2use std::io;
3
4/// Trait of helper objects to write out messages as bytes, for use with
5/// [`FramedWrite`].
6///
7/// [`FramedWrite`]: crate::codec::FramedWrite
8pub trait Encoder<Item> {
9    /// The type of encoding errors.
10    ///
11    /// [`FramedWrite`] requires `Encoder`s errors to implement `From<io::Error>`
12    /// in the interest of letting it return `Error`s directly.
13    ///
14    /// [`FramedWrite`]: crate::codec::FramedWrite
15    type Error: From<io::Error>;
16
17    /// Encodes a frame into the buffer provided.
18    ///
19    /// This method will encode `item` into the byte buffer provided by `dst`.
20    /// The `dst` provided is an internal buffer of the [`FramedWrite`] instance and
21    /// will be written out when possible.
22    ///
23    /// [`FramedWrite`]: crate::codec::FramedWrite
24    fn encode(&mut self, item: Item, dst: &mut BytesMut) -> Result<(), Self::Error>;
25}