#decode #encode #serialization #deserialize

no-std binrw

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

29 unstable releases

0.15.0 May 5, 2025
0.14.1 Oct 13, 2024
0.14.0 Jun 7, 2024
0.13.3 Nov 29, 2023
0.1.0 Sep 12, 2020

#71 in Encoding

Download history 74959/week @ 2025-09-23 82809/week @ 2025-09-30 117912/week @ 2025-10-07 100914/week @ 2025-10-14 101256/week @ 2025-10-21 90619/week @ 2025-10-28 88118/week @ 2025-11-04 88062/week @ 2025-11-11 108243/week @ 2025-11-18 79574/week @ 2025-11-25 119891/week @ 2025-12-02 119378/week @ 2025-12-09 104230/week @ 2025-12-16 47327/week @ 2025-12-23 46962/week @ 2025-12-30 128248/week @ 2026-01-06

345,072 downloads per month
Used in 302 crates (164 directly)

MIT license

205KB
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

~320–770KB
~16K SLoC