Skip to content

Commit

Permalink
Improve documentation and README
Browse files Browse the repository at this point in the history
  • Loading branch information
jam1garner committed Dec 11, 2019
1 parent fce34ed commit a46bcd3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "binwrite"
version = "0.1.4"
version = "0.1.5"
authors = ["jam1garner <jam1.mcleod@hotmail.com>"]
edition = "2018"
repository = "https://github.com/jam1garner/binwrite"
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,37 @@ A Rust crate for helping write structs as binary data using ✨macro magic✨

## Usage

The idea behind binwrite is using a derive macro for declaratively defining binary writing.
BinWrite uses a derive macro for declaratively defining binary writing methods for structs.

### Basic Example

```rust
use binwrite::BinWrite;

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

fn main() {
let point = Point { x: 1, y: -2 };
let rects = vec![
Rect { x: 1, y: -2, size: (3, 4) },
Rect { x: 20, y: 4, size: (5, 7) }
];
let mut bytes = vec![];
point.write(&mut bytes).unwrap();

assert_eq!(bytes, vec![1, 0, 0, 0, 0xFE, 0xFF, 0xFF, 0xFF]);
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,
]
);
}
```

Expand Down
40 changes: 38 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
//! A Rust crate for helping write structs as binary data using ✨macro magic✨
//!
//! # 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,
//! ]
//! );
//! }
//!```
use byteorder::{WriteBytesExt, BE, LE, NativeEndian};
use std::io::{Result, Write};

pub use binwrite_derive::*;
/// Derive macro for BinWrite. [Usage here](BinWrite).
pub use binwrite_derive::BinWrite;

/// Module for [WriteTrack\<T\>](write_track::WriteTrack)
pub mod write_track;
/// Built-in special writers (example: C strings)
pub mod writers;
mod binwrite_impls;

Expand Down Expand Up @@ -280,7 +316,7 @@ impl Into<String> for &Endian {
}
}

/// Options on how to write. Use [writer_option_new!](crate::writer_option_new) to create a new
/// Options on how to write. Use [writer_option_new!](writer_option_new) to create a new
/// instance. Manual initialization is not possible to prevent forward compatibility issues.
#[derive(Default, Clone)]
pub struct WriterOption {
Expand Down

0 comments on commit a46bcd3

Please sign in to comment.