#serialization #struct #deserialize #bits #binary-data #bit

no-std deku

bit level serialization/deserialization proc-macro for structs

26 releases (11 breaking)

0.16.0 Feb 28, 2023
0.15.1 Dec 19, 2022
0.15.0 Nov 16, 2022
0.13.1 Jun 9, 2022
0.1.2 Mar 30, 2020

#2 in #bits

Download history 5544/week @ 2023-12-08 4345/week @ 2023-12-15 2626/week @ 2023-12-22 3819/week @ 2023-12-29 5397/week @ 2024-01-05 5322/week @ 2024-01-12 5291/week @ 2024-01-19 5580/week @ 2024-01-26 5165/week @ 2024-02-02 5762/week @ 2024-02-09 4726/week @ 2024-02-16 6778/week @ 2024-02-23 5647/week @ 2024-03-01 5870/week @ 2024-03-08 4764/week @ 2024-03-15 4913/week @ 2024-03-22

22,203 downloads per month
Used in 34 crates (27 directly)

MIT/Apache

160KB
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.16"

no_std:

[dependencies]
deku = { version = "0.16", 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

~3MB
~70K SLoC