[−][src]Trait rustbreak::deser::DeSerializer
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 thiserror; extern crate serde; #[macro_use] use serde::de::Deserialize; use serde::Serialize; use std::io::Read; use rustbreak::deser::DeSerializer; use rustbreak::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) -> rustbreak::DeSerResult<Vec<u8>> { Ok(to_frobnar(val)) } fn deserialize<R: Read>(&self, s: R) -> rustbreak::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
fn serialize(&self, val: &T) -> DeSerResult<Vec<u8>>
Serializes a given value to a String
.
fn deserialize<R: Read>(&self, s: R) -> DeSerResult<T>
Deserializes a String
to a value.