Crates.io | binread |
lib.rs | binread |
version | 2.2.0 |
source | src |
created_at | 2020-04-03 19:33:25.611448 |
updated_at | 2021-08-25 04:40:07.174864 |
description | A Rust crate for helping read structs from binary data using ✨macro magic✨ |
homepage | |
repository | https://github.com/jam1garner/binread |
max_upload_size | |
id | 226029 |
size | 146,724 |
A Rust crate for helping parse structs from binary data using ✨macro magic✨
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")