Read and Write Data With Declarative ✨Macro Magic✨

binrw makes binary parsers and serializers easy to write, read, and maintain.

Star Fork
Explore More ⇩
Overview

binrw

Declarative

Skip the boilerplate. Declaring a type is all you need.

#[derive(BinRead, BinWrite)]
#[brw(little, magic = b"PK")]
struct ZipHeader {
    version: u16,
    flags: u16,
    comp_type: CompressionType,
    // ...
}

#[derive(BinRead, BinWrite)]
#[brw(repr(u16))]
enum CompressionType {
    None = 0,
    Shrunk = 1,
    Deflated = 8,
    Lzma = 14,
}

Simple definitions and easy-to-read by design to improve maintainability and approachability.

Powerful

Directives in attributes handle common binary parsing tasks like matching magic numbers, byte ordering, padding, alignment, data validation, and more.

#[derive(BinRead)]
struct StringTable {
    entry_count: u32,
    #[br(count = entry_count)]
    entries: Vec<NullString>,
}

Composable

With support for generics, custom parsers, and mapping functions, you can create your own building blocks as reusable types. And to keep you from reinventing the wheel we already include types you'll likely need:

Safe, Efficient and Embedded-Compatible