#format-string #struct #data #interface #strongly-typed #data-structures #create

structure

Use format strings to create strongly-typed data pack/unpack interfaces

3 releases

Uses old Rust 2015

0.1.2 Jan 11, 2018
0.1.1 Jun 11, 2017
0.1.0 Jun 10, 2017

#2183 in Encoding

Download history 1356/week @ 2024-07-21 796/week @ 2024-07-28 1117/week @ 2024-08-04 1028/week @ 2024-08-11 880/week @ 2024-08-18 1185/week @ 2024-08-25 747/week @ 2024-09-01 771/week @ 2024-09-08 749/week @ 2024-09-15 828/week @ 2024-09-22 929/week @ 2024-09-29 1163/week @ 2024-10-06 1157/week @ 2024-10-13 1139/week @ 2024-10-20 1274/week @ 2024-10-27 1100/week @ 2024-11-03

4,717 downloads per month
Used in 15 crates (7 directly)

MIT/Apache

11KB
53 lines

Structure

Use format strings to create strongly-typed data pack/unpack interfaces (inspired by Python's struct library).

Build Status Build status Crates.io

Documentation

Installation

Add this to your Cargo.toml:

[dependencies]
structure = "0.1"

And this to your crate root:

#[macro_use]
extern crate structure;

Examples

// Two `u32` and one `u8`
let s = structure!("2IB");
let buf: Vec<u8> = s.pack(1, 2, 3)?;
assert_eq!(buf, vec![0, 0, 0, 1, 0, 0, 0, 2, 3]);
assert_eq!(s.unpack(buf)?, (1, 2, 3));

It's useful to use pack_into and unpack_from when using types that implement Write or Read. The following example shows how to send a u32 and a u8 through sockets:

use std::net::{TcpListener, TcpStream};
let listener = TcpListener::bind("127.0.0.1:0")?;
let mut client = TcpStream::connect(listener.local_addr()?)?;
let (mut server, _) = listener.accept()?;
let s = structure!("IB");
s.pack_into(&mut client, 1u32, 2u8)?;
let (n, n2) = s.unpack_from(&mut server)?;
assert_eq!((n, n2), (1u32, 2u8));

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~215KB