Skip to main content

CustomSerialization

Trait CustomSerialization 

Source
pub trait CustomSerialization {
    // Required methods
    fn serialize<T>(
        &self,
        value: &T,
    ) -> Result<Vec<u8>, CustomSerializationError>
       where T: ?Sized + Serialize;
    fn deserialize<'a, T>(
        &self,
        s: &'a [u8],
    ) -> Result<T, CustomSerializationError>
       where T: Deserialize<'a>;
}
Expand description

Provides template for creating custom serializer

§Example Used in DefaultSerialization

use serde::{Deserialize, Serialize};
use flexbuffers;
use actix_telepathy::{ CustomSerialization, CustomSerializationError };

pub struct DefaultSerialization {}

impl CustomSerialization for DefaultSerialization {
    fn serialize<T>(&self, value: &T) -> Result<Vec<u8>, CustomSerializationError>
    where
        T: ?Sized + Serialize,
    {
        match flexbuffers::to_vec(value) {
            Ok(vec) => Ok(vec),
            Err(_) => Err(CustomSerializationError)
        }
    }

    fn deserialize<'a, T>(&self, s: &'a [u8]) -> Result<T, CustomSerializationError>
    where
        T: ?Sized + Deserialize<'a>,
    {
        match flexbuffers::from_slice(s) {
            Ok(val) => Ok(val),
            Err(_) => Err(CustomSerializationError)
        }
    }
}

§Telling Actix-Telepathy to use Custom Serializer

  • create a telepathy.yaml file in your crates root
  • add your custom struct’s name:
custom_serializer: "MySerializer"
  • import your custom struct whenever you are using the RemoteMessage derive macro.

Required Methods§

Source

fn serialize<T>(&self, value: &T) -> Result<Vec<u8>, CustomSerializationError>
where T: ?Sized + Serialize,

Source

fn deserialize<'a, T>(&self, s: &'a [u8]) -> Result<T, CustomSerializationError>
where T: Deserialize<'a>,

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§