pub trait DeSerializer<T: Serialize + DeserializeOwned>: Default + Send + Sync + Clone {
    fn serialize(&self, val: &T) -> DeSerResult<Vec<u8>>;
    fn deserialize<R: Read>(&self, s: R) -> DeSerResult<T>;
}
Expand description

A trait to bundle serializer and deserializer in a simple struct

It should preferably be an struct: one that does not have any members.

Example

For an imaginary serde compatible encoding scheme ‘Frobnar’, an example implementation can look like this:

extern crate daybreak;
extern crate thiserror;
extern crate serde;
#[macro_use]

use serde::de::Deserialize;
use serde::Serialize;
use std::io::Read;

use daybreak::deser::DeSerializer;
use daybreak::error;

#[derive(Clone, Debug, thiserror::Error)]
#[error("A frobnarizer could not splagrle.")]
struct FrobnarError;

fn to_frobnar<T: Serialize>(input: &T) -> Vec<u8> {
    unimplemented!(); // implementation not specified
}

fn from_frobnar<'r, T: Deserialize<'r> + 'r, R: Read>(input: &R) -> Result<T, FrobnarError> {
    unimplemented!(); // implementation not specified
}

#[derive(Debug, Default, Clone)]
struct Frobnar;

impl<T: Serialize> DeSerializer<T> for Frobnar
where
    for<'de> T: Deserialize<'de>,
{
    fn serialize(&self, val: &T) -> daybreak::DeSerResult<Vec<u8>> {
        Ok(to_frobnar(val))
    }

    fn deserialize<R: Read>(&self, s: R) -> daybreak::DeSerResult<T> {
        Ok(from_frobnar(&s).map_err(|e| error::DeSerError::Other(e.into()))?)
    }
}

fn main() {}

Important: You can only return custom errors if the other_errors feature is enabled

Required Methods

Serializes a given value to a String.

Deserializes a String to a value.

Implementors