26 releases

0.13.3 Nov 29, 2023
0.13.0 Oct 27, 2023
0.11.2 May 15, 2023
0.11.1 Feb 5, 2023
0.1.0 Sep 12, 2020

#35 in Encoding

Download history 9188/week @ 2023-12-11 13629/week @ 2023-12-18 10824/week @ 2023-12-25 15843/week @ 2024-01-01 10961/week @ 2024-01-08 15543/week @ 2024-01-15 12479/week @ 2024-01-22 15456/week @ 2024-01-29 11659/week @ 2024-02-05 16563/week @ 2024-02-12 16527/week @ 2024-02-19 18665/week @ 2024-02-26 17169/week @ 2024-03-04 17974/week @ 2024-03-11 14576/week @ 2024-03-18 13378/week @ 2024-03-25

63,916 downloads per month
Used in 110 crates (74 directly)

MIT license

195KB
4K SLoC

binrw

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

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

#[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")

For more information, including a more detailed overview of binrw, visit the documentation.

Dependencies

~1.5MB
~36K SLoC