Skip to content

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

sharksforarms/deku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

9d8ed00 · Mar 24, 2025
Mar 18, 2025
Oct 21, 2024
Mar 24, 2025
Mar 18, 2025
Sep 7, 2024
Dec 5, 2024
Mar 19, 2025
Mar 19, 2025
Jul 13, 2020
Sep 9, 2024
Feb 25, 2021
Mar 18, 2025
Jul 13, 2020
Jul 13, 2020
Mar 18, 2025
Oct 21, 2020
May 23, 2024

Deku

Latest Version Rust Documentation Actions Status codecov

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

Compiler support: requires rustc 1.81+

[dependencies]
deku = "0.18"

no_std:

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