7 releases

0.7.1 Jul 31, 2020
0.6.1 Jul 6, 2020
0.1.2 Mar 30, 2020

#124 in Encoding

Download history 4/week @ 2020-04-17 2/week @ 2020-04-24 2/week @ 2020-05-01 10/week @ 2020-05-22 68/week @ 2020-05-29 90/week @ 2020-06-05 89/week @ 2020-06-12 94/week @ 2020-06-19 224/week @ 2020-06-26 100/week @ 2020-07-03 175/week @ 2020-07-10 22/week @ 2020-07-17 68/week @ 2020-07-24 85/week @ 2020-07-31

317 downloads per month

MIT/Apache

79KB
1.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

[dependencies]
deku = "0.7"

no_std:

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

Example

See documentation or examples folder for more!

Simple example, let's read big-endian data into a struct, with fields containing different sizes, modify a value, and write it back

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

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.5MB
~32K SLoC