simple_parse
simple_parse is a declarative encoder/decoder for Rust structs to/from binary.
It provides basic implementations for most standard Rust types and also provides a derive macro to automatically implement the trait on your own Rust types !
For lower level control, take a look at deku.
Usage
use ;
let mut cursor: & = &;
// Decode bytes into a struct
let mut my_struct = from_bytes?;
/// Modify the struct
my_struct.items.push;
/// Encode modified struct into bytes
let mut dst_buf: = Vec new;
my_struct.to_bytes?;
//dst_buf == [0x01,0x00,0x03,0xDE,0xAD,0xBE,0xEF,0xBA,0xDC,0x0F,0xFE,0xFF,0xFF,0xFF,0xFF]
For complete examples see : examples
Default Impls
Type | Encoded size |
---|---|
u8|u16|u32|u64|u128|usize | 1,2,4,8,16,sizeof(usize) |
i8|i16|i32|i64|i128|isize | 1,2,4,8,16,sizeof(usize) |
raw ptr | sizeof(usize) |
bool | 1 |
String | sizeof(u64) + str.len() |
CString | str.len() + 1 |
Vec<T> | sizeof(u64) + [vec.len() * sizeof(T)] |
HashSet<K> | sizeof(u64) + [set.len() * sizeof(K)] |
HashMap<K,V> | sizeof(u64) + [map.len() * (sizeof(K) + sizeof(V))] |