#bits #serialization #deserialization #struct #deku

no-std deku

bit level serialization/deserialization proc-macro for structs

18 releases (7 breaking)

0.12.2 Jun 15, 2021
0.12.0 Apr 13, 2021
0.11.0 Feb 25, 2021
0.9.3 Dec 14, 2020
0.1.2 Mar 30, 2020

#78 in Encoding

Download history 107/week @ 2021-02-26 57/week @ 2021-03-05 76/week @ 2021-03-12 89/week @ 2021-03-19 82/week @ 2021-03-26 60/week @ 2021-04-02 102/week @ 2021-04-09 128/week @ 2021-04-16 60/week @ 2021-04-23 43/week @ 2021-04-30 35/week @ 2021-05-07 85/week @ 2021-05-14 53/week @ 2021-05-21 78/week @ 2021-05-28 52/week @ 2021-06-04 86/week @ 2021-06-11

313 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

215KB
4K 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.12"

no_std:

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

~2MB
~46K SLoC