Crate format_struct

source ·
Expand description

A crate for quick and easy format structure definitions for use in binary file parsing.

§Usage

This crate should be used by invoking the provided format_struct macro like this:

use format_struct::{format_struct, ReprByteSlice};

// Here we define a small structure.
format_struct! {
    struct little Test {
        foo: u8,
        bar: u32,
        baz: [u8; 2],
    }
}

// This is the data we want to parse:
let data = &[
    0x42u8, // this goes into foo
    0x39, 0x05, 0x00, 0x00, // this goes into bar
    0xaa, 0x55, // this goes into baz
][..];

// This is completely zero-cost since the implementation is just a transmute.
let s = Test::from_byte_slice(data).unwrap();

// Each integer field access compiles to a single unaligned memory access instruction.
assert_eq!(s.foo, 0x42);
assert_eq!(s.bar.get(), 1337);
assert_eq!(&s.baz, &[0xaa, 0x55]);

Re-exports§

Modules§

  • Provides types and traits for representing endianness of an integer encoding.

Macros§

  • Defines a structure that can be transmuted from/into a byte slice for parsing/constructing binary formats in a zero-copy way.

Structs§

  • A type that wraps a byte array to be decoded into a i16.
  • A type that wraps a byte array to be decoded into a i32.
  • A type that wraps a byte array to be decoded into a i64.
  • A type that wraps a byte array to be decoded into a i128.
  • The error type returned when a type is transmuted into a byte slice and the multiple of the slice’s length and the type’s size overflows isize.
  • A type that wraps a byte array to be decoded into a u16.
  • A type that wraps a byte array to be decoded into a u32.
  • A type that wraps a byte array to be decoded into a u64.
  • A type that wraps a byte array to be decoded into a u128.
  • The error type returned when a byte slice of size that is either not equal to or not a multiple of the target type’s size is transmuted into that type.

Traits§

  • An unsafe trait for types that may be safely transmuted from and to byte slices.

Functions§

  • Multiplies a size of a type with a count and returns an error in case the resulting value overflows isize.
  • Checks that the specified size if a multiple of a type’s size and returns the size divided by the type’s size or an error if that is not the case.