restruct
is used to interpret binary data stored in files or other sources or convert
between C structs and Rust types and when using a parser-generator is considered
disproportionate.
It is a brainchild of Python's struct-module.
The library uses Format Strings as compact descriptions of the binary data and the intended conversion to/from Rust-types. The Format Strings are interpreted at compile-time to generate a type whose functions can be used to convert between unstructured and structured data.
// Generate a parser in little-endian for two 32bit integers, a float and a bool.
;
// Pack a tuple of two integers, a float an a bool into a [u8; _]-buffer.
let packed = pack;
assert_eq!;
// Packing and unpacking can't fail at runtime.
let unpacked = unpack;
assert_eq!;
// Because packing and unpacking is const we can use these functions to initialize other consts
const TEAPOT: Unpacked = unpack;
const TEAPOT_TEMPERATURE: i32 = TEAPOT.0;
const TEAPOT_FILL_STATUS: f32 = TEAPOT.2;
const TEAPOT_ACTIVE: bool = TEAPOT.3;