Trait DecodeMethod

Source
pub trait DecodeMethod<T> {
    type Error: 'static + Error;

    // Required method
    fn decode(bytes: BytesMut) -> Result<T, Self::Error>;
}
Expand description

Trait for decoding given bytes into an item.

Generic parameter T is the data that will be decoded. When implementing this trait, you can specify trait requirements for T, so that, any types that implements that trait will be able to be decoded.

§Example

Below example will deserialize any item that implements serde::de::DeserializeOwned from json bytes.

use tsyncp::util::codec::DecodeMethod;
use bytes::BytesMut;

pub struct MyCustomCodec;

impl<T: serde::de::DeserializeOwned> DecodeMethod<T> for MyCustomCodec {
    type Error = serde_json::Error;

    fn decode(bytes: BytesMut) -> Result<T, Self::Error> {
        serde_json::from_slice(bytes.as_ref())
    }
}

Required Associated Types§

Source

type Error: 'static + Error

Error returned by associated method decode(_).

Required Methods§

Source

fn decode(bytes: BytesMut) -> Result<T, Self::Error>

Decode given data.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§