binrw

binrw helps you write maintainable & easy-to-read declarative binary data
readers and writers using ✨macro magic✨.
Features
- Generates efficient data parsers and serialisers for structs and enums using
#[derive]
- Reads and writes data from any source using standard
io::Read
and
io::Write
streams
- Directives in attributes
handle common binary parsing tasks like matching magic numbers, byte ordering,
padding & alignment, data validation, and more
- Includes reusable types for common data structures like
null-terminated strings and
data indirection using offsets
- Parses types from third-party crates using
free functions
or value maps
- Uses efficient in-memory representations (does not require
#[repr(C)]
or
#[repr(packed)]
)
- Code in attributes is written as code, not as strings, for improved ergonomics
and first-class IDE support
- Supports no_std
Usage
```rust
[derive(BinRead)]
[br(magic = b"DOG", assert(name.len() != 0))]
struct Dog {
bonepilecount: 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.readne().unwrap();
asserteq!(dog.bonepiles, &[0x1, 0x12]);
asserteq!(dog.name.into_string(), "Rudy")
```
For more information, including a more detailed overview of binrw,
visit the documentation.