Trait EncodeMethod

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

    // Required method
    fn encode(data: &T) -> Result<Bytes, Self::Error>;
}
Expand description

Trait for encoding a given item into bytes.

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

§Example

Below example will serialize any item that implements serde::Serialize into json.

use tsyncp::util::codec::EncodeMethod;
use bytes::Bytes;

pub struct MyCustomCodec;

impl<T: serde::Serialize> EncodeMethod<T> for MyCustomCodec {
    type Error = serde_json::Error;

    fn encode(data: &T) -> Result<Bytes, Self::Error> {
        serde_json::to_vec(data).map(Into::into)
    }
}

Required Associated Types§

Source

type Error: 'static + Error

Error returned by associated method encode(_).

Required Methods§

Source

fn encode(data: &T) -> Result<Bytes, Self::Error>

Encode 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§