Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jam1garner/binwrite
A Rust crate for helping write structs as binary data using ✨macro magic✨
https://github.com/jam1garner/binwrite
Last synced: 4 days ago
JSON representation
A Rust crate for helping write structs as binary data using ✨macro magic✨
- Host: GitHub
- URL: https://github.com/jam1garner/binwrite
- Owner: jam1garner
- License: mit
- Created: 2019-11-02T16:20:06.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-17T20:23:45.000Z (over 4 years ago)
- Last Synced: 2025-01-03T02:53:58.557Z (5 days ago)
- Language: Rust
- Size: 22.5 KB
- Stars: 18
- Watchers: 3
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# binwrite
[![Join the chat at https://gitter.im/binwrite/community](https://badges.gitter.im/binwrite/community.svg)](https://gitter.im/binwrite/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
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
```rust
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.](https://docs.rs/binwrite/0.1/binwrite/trait.BinWrite.html)