[−][src]Crate deku
Deku serialization/deserialization library supporting bit level granularity.
This crate allows you define the structure of data, consume from a stream of bytes and writing it back to it's raw form. This allows the developer to focus on building and maintaining the representation of data and not on serialization/deserialization code.
This approach is especially usefull when dealing with binary structures or network protocols
Under the hood, it makes use of the bitvec crate as the "Reader" and “Writer”
Example
use deku::prelude::*; #[derive(Debug, PartialEq, DekuRead, DekuWrite)] #[deku(endian = "big")] struct DekuTest { #[deku(bits = "4")] field_a: u8, #[deku(bits = "4")] field_b: u8, field_c: u16, } let data: &[u8] = [0b0110_1001, 0xBE, 0xEF].as_ref(); let (_rest, mut val) = DekuTest::from_bytes((data, 0)).unwrap(); assert_eq!(DekuTest { field_a: 0b0110, field_b: 0b1001, field_c: 0xBEEF, }, val); val.field_c = 0xC0FE; let data_out = val.to_bytes(); assert_eq!(vec![0b0110_1001, 0xC0, 0xFE], data_out);
Modules
error | |
prelude |
Traits
BitsReader | "Reader" trait: read bits and construct type |
BitsReaderItems | "Reader" trait: read bits and construct multiple of type |
BitsWriter | "Writer" trait: write from type to bits |
Derive Macros
DekuRead | |
DekuWrite |