pub trait Decode {
// Provided method
fn decode(bytes: &[u8]) -> Result<Self, Box<dyn Error>>
where Self: Sized + for<'a> Deserialize<'a> { ... }
}Expand description
Adds deserialisation via bincode and serde to a type.
Must be present on all major file types.
use std::error::Error;
use ot_tools_io::Decode;
#[derive(serde::Deserialize, std::fmt::Debug, PartialEq)]
struct SomeType {
x: u8,
y: u32,
}
// default implementation
impl Decode for SomeType {}
let bytes: Vec<u8> = vec![8, 127, 95, 245, 1];
let decoded = SomeType::decode(&bytes).unwrap();
assert_eq!(
SomeType { x : 8, y: 32857983 },
decoded,
);