#macro #binary-data #parser #magic-macro #struct #helping #macro-derive #methods #u16 #u8

no-std binread

A Rust crate for helping read structs from binary data using ✨macro magic✨

11 stable releases

2.2.0 Aug 25, 2021
2.1.1 Apr 1, 2021
2.1.0 Mar 28, 2021
1.4.1 Nov 11, 2020
1.0.2 Jul 15, 2020

#1028 in Rust patterns

Download history 16162/week @ 2025-03-10 17676/week @ 2025-03-17 15270/week @ 2025-03-24 13973/week @ 2025-03-31 16222/week @ 2025-04-07 12853/week @ 2025-04-14 12379/week @ 2025-04-21 13028/week @ 2025-04-28 15992/week @ 2025-05-05 14915/week @ 2025-05-12 16634/week @ 2025-05-19 14563/week @ 2025-05-26 13822/week @ 2025-06-02 16936/week @ 2025-06-09 22122/week @ 2025-06-16 23763/week @ 2025-06-23

77,099 downloads per month
Used in 330 crates (35 directly)

MIT license

115KB
2K SLoC

binread

tests docs.rs codecov discord matrix: #binrw:matrix.org

A Rust crate for helping parse structs from binary data using ✨macro magic✨

Usage

BinRead uses a derive macro for declaratively defining binary parsing methods for structs.

#[derive(BinRead)]
#[br(magic = b"DOG", assert(name.len() != 0))]
struct Dog {
    bone_pile_count: u8,

    #[br(big, count = bone_pile_count)]
    bone_piles: Vec<u16>,

    #[br(align_before = 0xA)]
    name: NullString
}

let mut reader = Cursor::new(b"DOG\x02\x00\x01\x00\x12\0\0Rudy\0");
let dog: Dog = reader.read_ne().unwrap();
assert_eq!(dog.bone_piles, &[0x1, 0x12]);
assert_eq!(dog.name.into_string(), "Rudy")

More documentation can be found here

Dependencies

~1.5MB
~39K SLoC