#binary-data #structs #write #helping #i32 #magic✨ #✨macro

binwrite

A Rust crate for helping write structs as binary data using ✨macro magic✨

8 releases

0.2.1 Feb 7, 2020
0.2.0 Feb 7, 2020
0.1.5 Dec 11, 2019
0.1.3 Nov 3, 2019

#2266 in Encoding

Download history 1462/week @ 2024-07-22 1007/week @ 2024-07-29 1050/week @ 2024-08-05 801/week @ 2024-08-12 744/week @ 2024-08-19 708/week @ 2024-08-26 412/week @ 2024-09-02 536/week @ 2024-09-09 930/week @ 2024-09-16 599/week @ 2024-09-23 312/week @ 2024-09-30 496/week @ 2024-10-07 412/week @ 2024-10-14 396/week @ 2024-10-21 537/week @ 2024-10-28 188/week @ 2024-11-04

1,573 downloads per month
Used in 20 crates (13 directly)

MIT license

19KB
225 lines

binwrite

A Rust crate for helping write structs as binary data using ✨macro magic✨

Usage

BinWrite uses a derive macro for declaratively defining binary writing methods for structs.

Basic Example

use binwrite::BinWrite;

#[derive(BinWrite)]
#[binwrite(little)]
struct Rect {
    x: i32,
    y: i32,
    #[binwrite(big)]
    size: (u16, u16),
}

fn main() {
    let rects = vec![
        Rect { x: 1, y: -2, size: (3, 4) },
        Rect { x: 20, y: 4, size: (5, 7) }
    ];
    let mut bytes = vec![];
    rects.write(&mut bytes).unwrap();
    assert_eq!(
        bytes,
        vec![
        //  [  x (little endian) ]  [  y (little endian) ]  [ size.0 ]  [ size.1 ]
            0x01, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x03, 0x00, 0x04,
            0x14, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x07,
        ]
    );
}

more examples can be found in the BinWrite documentation.

Dependencies

~1.5MB
~37K SLoC