Crate bin_layout
source · [−]Expand description
Very fast!and flexible, This library used to serialize and deserialize data in binary format.
Inspaired by bincode, But much more flexible.
Endianness
By default, the library uses little endian.
If you want to use big endian, you can set BE features flag. And for native endian use NE. For example:
[dependencies]
bin-layout = { version = "1", features = ["BE"] }Data Type
The library is very flexible and easy to use. The only trait you need to implement is DataType.
All primitive types implement this trait.
And For collection types, Vec and String are supported. They are encoded with their length u32 value first, Following by each entry of the collection.
Example
use bin_layout::DataType;
#[derive(DataType)]
struct Car { name: String, year: u16, is_new: bool }
#[derive(DataType)]
struct Company { name: String, cars: Vec<Car> }
let company = Company {
name: "Tesla".into(),
cars: vec![
Car { name: "Model S".into(), year: 2018, is_new: true },
Car { name: "Model X".into(), year: 2019, is_new: false },
],
};
let mut view = [0; 64].into();
company.serialize(&mut view);
assert_eq!(view.offset, 41); // 41 bytes were written
view.offset = 0; // reset offset
let company = Company::deserialize(&mut view).unwrap();Re-exports
pub use data_view;Structs
This struct represents a data view for reading and writing data in a byte array. When read/write, This increment current offset by the size of the value.
This utility struct use for serialize or deserialize variable length records.
Enums
Traits
Type Definitions
Shortcut for Result<T, bin_layout::ErrorKind>