2018-02-12 21:10:19 +04:00
|
|
|
use bytes::BytesMut;
|
2019-02-21 11:56:15 -08:00
|
|
|
use std::io;
|
2018-02-12 21:10:19 +04:00
|
|
|
|
|
|
|
|
/// Trait of helper objects to write out messages as bytes, for use with
|
|
|
|
|
/// `FramedWrite`.
|
2018-06-04 22:36:06 -05:00
|
|
|
|
|
|
|
|
// Note: We can't deprecate this trait, because the deprecation carries through to tokio-codec, and
|
|
|
|
|
// there doesn't seem to be a way to un-deprecate the re-export.
|
2018-02-12 21:10:19 +04:00
|
|
|
pub trait Encoder {
|
|
|
|
|
/// The type of items consumed by the `Encoder`
|
|
|
|
|
type Item;
|
|
|
|
|
|
|
|
|
|
/// The type of encoding errors.
|
|
|
|
|
///
|
|
|
|
|
/// `FramedWrite` requires `Encoder`s errors to implement `From<io::Error>`
|
|
|
|
|
/// in the interest letting it return `Error`s directly.
|
|
|
|
|
type Error: From<io::Error>;
|
|
|
|
|
|
|
|
|
|
/// Encodes a frame into the buffer provided.
|
|
|
|
|
///
|
|
|
|
|
/// This method will encode `item` into the byte buffer provided by `dst`.
|
|
|
|
|
/// The `dst` provided is an internal buffer of the `Framed` instance and
|
|
|
|
|
/// will be written out when possible.
|
2019-02-21 11:56:15 -08:00
|
|
|
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error>;
|
2018-02-12 21:10:19 +04:00
|
|
|
}
|