#deku #bits #serialization #deserialization #struct

no-std deku

bit level serialization/deserialization proc-macro for structs

21 releases

0.13.1 Jun 9, 2022
0.13.0 Feb 27, 2022
0.12.6 Jun 9, 2022
0.12.5 Nov 11, 2021
0.1.2 Mar 30, 2020

#47 in Encoding

Download history 225/week @ 2022-03-09 475/week @ 2022-03-16 370/week @ 2022-03-23 164/week @ 2022-03-30 363/week @ 2022-04-06 290/week @ 2022-04-13 247/week @ 2022-04-20 286/week @ 2022-04-27 366/week @ 2022-05-04 475/week @ 2022-05-11 211/week @ 2022-05-18 226/week @ 2022-05-25 365/week @ 2022-06-01 306/week @ 2022-06-08 220/week @ 2022-06-15 178/week @ 2022-06-22

1,089 downloads per month
Used in 13 crates (12 directly)

MIT/Apache

145KB
2.5K SLoC

Deku

Latest Version Rust Documentation Actions Status codecov Gitter

Declarative binary reading and writing

This crate provides bit-level, symmetric, serialization/deserialization implementations for structs and enums

Why use Deku

Productivity: 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

Usage

[dependencies]
deku = "0.13"

no_std:

[dependencies]
deku = { version = "0.13", default-features = false, features = ["alloc"] }

Example

See documentation or examples folder for more!

Read big-endian data into a struct, modify a value, and write it

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,
}

fn main() {
    let data: Vec<u8> = vec![0b0110_1001, 0xBE, 0xEF];
    let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 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().unwrap();
    assert_eq!(vec![0b0110_1001, 0xC0, 0xFE], data_out);
}

Changelog

See CHANGELOG.md

Dependencies

~1.4–2MB
~45K SLoC