29 releases

0.18.1 Sep 9, 2024
0.17.0 May 23, 2024
0.16.0 Feb 28, 2023
0.15.1 Dec 19, 2022
0.1.2 Mar 30, 2020

#94 in Encoding

Download history 10066/week @ 2024-08-08 8927/week @ 2024-08-15 9876/week @ 2024-08-22 9700/week @ 2024-08-29 9384/week @ 2024-09-05 9174/week @ 2024-09-12 9933/week @ 2024-09-19 9268/week @ 2024-09-26 8865/week @ 2024-10-03 11717/week @ 2024-10-10 13475/week @ 2024-10-17 13551/week @ 2024-10-24 12159/week @ 2024-10-31 10705/week @ 2024-11-07 11633/week @ 2024-11-14 10834/week @ 2024-11-21

47,448 downloads per month
Used in 50 crates (33 directly)

MIT/Apache

220KB
3.5K SLoC

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.71+

[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

Dependencies

~0.8–1.8MB
~37K SLoC