Trait rustbreak::deser::DeSerializer[][src]

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

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 rustbreak;
extern crate serde;
#[macro_use] extern crate failure;

use std::io::Read;
use serde::Serialize;
use serde::de::Deserialize;
use failure::{Context, Fail, Backtrace};

use rustbreak::error;
use rustbreak::deser::DeSerializer;

#[derive(Fail, Debug)]
#[fail(display = "A FrobnarError ocurred")]
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) -> Result<Vec<u8>, failure::Error> {
        Ok(to_frobnar(val))
    }

    fn deserialize<R: Read>(&self, s: R) -> Result<T, failure::Error> {
        Ok(from_frobnar(&s)?)
    }
}

fn main() {}

Required Methods

Serializes a given value to a String

Deserializes a String to a value

Implementors