Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Codecs

A dnet codec is responsible for converting messages between Rust values and wire format.

dnet separates encoding and decoding into two traits, then combines them into a single Codec trait.

Encode

/// Trait for encoders.
pub trait Encode {
    /// Error type.
    type Error: std::error::Error;

    /// Encode message into writer.
    fn encode<W, T>(&mut self, writer: W, message: &T) -> Result<(), Self::Error>
    where
        W: Write,
        T: Serialize;
}

Encode takes a writable sink and a serializable message. Implementations may write messages in any wire format, such as JSON, binary, or length-delimited frames.

Decode

/// Trait for decoders.
pub trait Decode {
    /// Error type.
    type Error: std::error::Error;

    /// Decode message from reader.
    fn decode<R, T>(&mut self, data: R) -> Result<T, Self::Error>
    where
        R: Read,
        for<'de> T: Deserialize<'de>;
}

Decode reads bytes from a readable source and produces a deserialized message value. The generic error type allows decoding failures to be reported precisely.

Codec

/// Trait for `dnet` codecs.
pub trait Codec: Encode + Decode {}

Codec is a simple alias trait combining Encode and Decode. A dnet transport uses a Codec to transform outgoing messages into bytes and incoming bytes back into message values.

Provided codecs

dnet ships with the following codecs in dnet’s codecs module (behind the “codecs” feature, enabled by default):

  • BincodeCodec
    • Binary codec using bincode.
    • Best for compact, fast wire format when size is important.
  • JsonCodec
    • Text codec using serde_json.
    • Best for readable, interoperable payloads and debugging.

In addition, dnet includes length-delimiting framing codec for use with the FramedTransport: