Declarative binary reading and writing
This crate provides bit-level, symmetric, serialization/deserialization implementations for structs and enums
Productivity: Focus and declare your types that represent the data, Deku will generate symmetric reader/writer functions for your type! Avoid the requirement of writing redundant, error-prone parsing and writing code for binary structs or network headers
toml
[dependencies]
deku = "0.2"
See examples for more!
```rust use deku::prelude::*; use std::convert::TryFrom;
struct FieldF { #[deku(bits = "6")] data: u8, }
/// DekuTest Struct // 0 1 2 3 4 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | fielda | fieldb |c| field_d | e | f | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //
// #[deku(endian = "little")] // By default it uses the system endianness, but can be overwritten
struct DekuTest {
fielda: u8,
#[deku(bits = "7")]
fieldb: u8,
#[deku(bits = "1")]
fieldc: u8,
#[deku(endian = "big")]
fieldd: u16,
#[deku(bits = "2")]
fielde: u8,
fieldf: FieldF,
numitems: u8,
#[deku(len = "numitems", endian = "big")]
items: Vec
fn main() { let testdata: &[u8] = [ 0xAB, 0b10100101, 0xAB, 0xCD, 0b11000110, 0x02, 0xBE, 0xEF, 0xC0, 0xFE, ] .asref();
let test_deku = DekuTest::try_from(test_data).unwrap();
assert_eq!(
DekuTest {
field_a: 0xAB,
field_b: 0b0_1010010,
field_c: 0b0000000_1,
field_d: 0xABCD,
field_e: 0b0000_0011,
field_f: FieldF { data: 0b00_000110 },
num_items: 2,
items: vec![0xBEEF, 0xC0FE],
},
test_deku
);
let test_deku: Vec<u8> = test_deku.into();
assert_eq!(test_data.to_vec(), test_deku);
} ```