Skip to main content

SerdeAPI

Trait SerdeAPI 

Source
pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
    // Provided methods
    fn to_file(&self, filename: &str) -> Result<(), Error> { ... }
    fn from_file(filename: &str) -> Result<Self, Error>
       where Self: Sized + for<'de> Deserialize<'de> { ... }
    fn to_json(&self) -> String { ... }
    fn from_json(json_str: &str) -> Result<Self, Error> { ... }
    fn to_yaml(&self) -> String { ... }
    fn from_yaml(yaml_str: &str) -> Result<Self, Error> { ... }
    fn to_bincode(&self) -> Vec<u8>  { ... }
    fn from_bincode(encoded: &[u8]) -> Result<Self, Error> { ... }
}

Provided Methods§

Source

fn to_file(&self, filename: &str) -> Result<(), Error>

Save current data structure to file. Method adaptively calls serialization methods dependent on the suffix of the file given as str.

§Argument:
  • filename: a str storing the targeted file name. Currently .json and .yaml suffixes are supported
§Returns:

A Rust Result

Source

fn from_file(filename: &str) -> Result<Self, Error>
where Self: Sized + for<'de> Deserialize<'de>,

Read from file and return instantiated struct. Method adaptively calls deserialization methods dependent on the suffix of the file name given as str. Function returns a dynamic Error Result if it fails.

§Argument:
  • filename: a str storing the targeted file name. Currently .json and .yaml suffixes are supported
§Returns:

A Rust Result wrapping data structure if method is called successfully; otherwise a dynamic Error.

Source

fn to_json(&self) -> String

json serialization method.

Source

fn from_json(json_str: &str) -> Result<Self, Error>

json deserialization method.

Source

fn to_yaml(&self) -> String

yaml serialization method.

Source

fn from_yaml(yaml_str: &str) -> Result<Self, Error>

yaml deserialization method.

Source

fn to_bincode(&self) -> Vec<u8>

bincode serialization method.

Source

fn from_bincode(encoded: &[u8]) -> Result<Self, Error>

bincode deserialization method.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> SerdeAPI for T
where T: Serialize + for<'a> Deserialize<'a>,