simple_parse

crates.io mio Lines of Code

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

```Rust use ::simple_parse::{SpRead, SpWrite};

[derive(SpRead, SpWrite)]

pub struct SomeStruct { pub some_field: u8, nitems: u16, #[sp(count="nitems", endian="big")] pub items: Vec, }

let mut cursor: &[u8] = &[ 0x01, // some_field 0x00,0x02, // nitems (Field holding the number of Vec items) 0xDE,0xAD,0xBE,0xEF, // items[0] 0xBA,0xDC,0x0F,0xFE // items[1] ];

// Decode bytes into a struct let mut mystruct = SomeStruct::frombytes(&mut cursor)?;

/// Modify the struct my_struct.items.push(0xFFFFFFFF);

/// Encode modified struct into bytes let mut dstbuf: Vec = Vec::new(); mystruct.tobytes(&mut dstbuf)?; //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\ | sizeof(u64) + [vec.len() * sizeof(T)] | | HashSet\ | sizeof(u64) + [set.len() * sizeof(K)] | | HashMap\ | sizeof(u64) + [map.len() * (sizeof(K) + sizeof(V))] |