Trait tsyncp::util::codec::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.

Object Safety§

This trait is not object safe.

Implementors§