#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

#1578 in Encoding

Download history 1397/week @ 2024-03-14 1043/week @ 2024-03-21 1326/week @ 2024-03-28 1016/week @ 2024-04-04 1336/week @ 2024-04-11 1649/week @ 2024-04-18 1360/week @ 2024-04-25 1250/week @ 2024-05-02 1226/week @ 2024-05-09 1311/week @ 2024-05-16 1040/week @ 2024-05-23 1209/week @ 2024-05-30 1095/week @ 2024-06-06 961/week @ 2024-06-13 1182/week @ 2024-06-20 950/week @ 2024-06-27

4,491 downloads per month
Used in 14 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

~220KB