Trait bin_layout::DataType
source · [−]pub trait DataType<'de>: Sized {
fn serialize(self, _: &mut Cursor<impl AsMut<[u8]>>) -> Result<()>;
fn deserialize(_: &mut Cursor<&'de [u8]>) -> Result<Self>;
fn encode(self, data: &mut [u8]) -> Result<()> { ... }
fn decode(data: &'de [u8]) -> Result<Self> { ... }
}Expand description
A trait for serialize and deserialize data for binary format.
All primitive types implement this trait.
Vec, String, &[T], &str etc.. are encoded with their length value first, Following by each entry.
Required methods
Serialize the data to binary format.
Provided methods
Shortcut for DataType::serialize(self, &mut View::new(bytes.as_mut()))
Example
use bin_layout::DataType;
#[derive(DataType)]
struct FooBar {
foo: u8,
bar: [u8; 2],
}
let mut bytes = [0; 3];
FooBar { foo: 1, bar: [2, 3] }.encode(&mut bytes);
assert_eq!(bytes, [1, 2, 3]);Shortcut for DataType::deserialize(&mut View::new(bytes.as_ref()))
Example
use bin_layout::DataType;
#[derive(DataType, PartialEq, Debug)]
struct FooBar {
foo: u8,
bar: [u8; 2],
}
let foobar = FooBar::decode(&[1, 2, 3]).unwrap();
assert_eq!(foobar, FooBar { foo: 1, bar: [2, 3] });