Encode

Trait Encode 

Source
pub trait Encode {
    // Provided method
    fn encode(&self) -> Result<Vec<u8>, Box<dyn Error>>
       where Self: Serialize { ... }
}
Expand description

Adds serialization via bincode and serde to a type. Must be present on all major file types.

use std::error::Error;
use ot_tools_io::Encode;

#[derive(serde::Serialize, std::fmt::Debug)]
struct SomeType {
    x: u8,
    y: u32,
}

// default implementation
impl Encode for SomeType {}

let x = SomeType { x : 8, y: 32857983 };
let encoded = x.encode().unwrap();
assert_eq!(
    vec![8, 127, 95, 245, 1],
    encoded,
);

Provided Methods§

Source

fn encode(&self) -> Result<Vec<u8>, Box<dyn Error>>
where Self: Serialize,

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§