Decoder

Trait Decoder 

Source
pub trait Decoder {
    type Result;
    type Error: Error;

    // Required method
    fn decode(&self, bytes: &[u8]) -> Result<Self::Result, Self::Error>;
}
Expand description

Represents a way of decoding a payload of an incoming message (which is received as a sequence of bytes) into an arbitrary result type.

It is important to know that both the original bytes (Vec<u8>) and the decoded Result will be owned by the same Envelope. Given that Rust doesn’t allow self-referential structs, we have to keep in mind that the result type may not contain references to the original bytes.

Also, since the envelope is not easily destructed, only a reference to the decoded payload is exposed.

For cases, where references to the original bytes are needed in the decoded result, or where the decoded result must be owned by the external logic, the byte slice may be accessed on the envelope for manual decoding, and the provided NoopDecoder may be used as a dud.

Required Associated Types§

Source

type Result

The type of decoded result.

Source

type Error: Error

The type of error produced when decoding is not possible.

Required Methods§

Source

fn decode(&self, bytes: &[u8]) -> Result<Self::Result, Self::Error>

Decodes the given sequence of bytes into the desired Result, or returns an appropriate Error.

Implementors§

Source§

impl Decoder for NoopDecoder

Source§

impl Decoder for StringDecoder

Source§

impl<F, R, E> Decoder for F
where F: Fn(&[u8]) -> Result<R, E>, E: Error,

Implements Decoder for any function or closure that returns a non-referential Result.

If the result references the given bytes, this implementation will not work. See the Decoder documentation for more details.

Source§

type Result = R

Source§

type Error = E