tokio_io/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// Note: We can't deprecate this trait, because the deprecation carries through to tokio-codec, and
8// there doesn't seem to be a way to un-deprecate the re-export.
9pub trait Encoder {
10 /// The type of items consumed by the `Encoder`
11 type Item;
12
13 /// The type of encoding errors.
14 ///
15 /// `FramedWrite` requires `Encoder`s errors to implement `From<io::Error>`
16 /// in the interest letting it return `Error`s directly.
17 type Error: From<io::Error>;
18
19 /// Encodes a frame into the buffer provided.
20 ///
21 /// This method will encode `item` into the byte buffer provided by `dst`.
22 /// The `dst` provided is an internal buffer of the `Framed` instance and
23 /// will be written out when possible.
24 fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error>;
25}