#overlay #struct #transmute #macro #reinterpret #debugging

macro overlay_macro

A proc-macro for implementing Overlay, to reinterpret a slice of bytes as a bit/byte packed struct

6 stable releases

2.0.0 Jul 3, 2024
1.2.0 Jun 20, 2024
1.1.1 Jun 20, 2024
1.1.0 May 31, 2024
1.0.1 May 30, 2024

#634 in Embedded development

Download history 346/week @ 2024-05-30 22/week @ 2024-06-06 14/week @ 2024-06-13 276/week @ 2024-06-20 120/week @ 2024-06-27 97/week @ 2024-07-04 2/week @ 2024-07-11 4/week @ 2024-07-18

386 downloads per month

GPL-3.0-or-later

25KB
434 lines

A proc-macro for generating a struct which can be overlaid. See the documentation for usage and examples.

Usage

#[overlay]
#[derive(Clone, Debug)]
pub struct InquiryCommand {
    #[overlay(byte=0, bits=0..8)]
    pub op_code: u8,

    #[overlay(byte=1)]
    pub enable_vital_product_data: bool,

    #[overlay(byte=2, bits=0..=7)]
    pub page_code: u8,

    #[overlay(bytes=3..=4, bits=0..=7)]
    pub allocation_length: u16,

    ...
}

This will create a wrapper struct around an array of bytes, with generated getters and setters for each "field", accessing the bytes/bits at the given offsets.

Todo

  • Support for nested structs
  • Support for individual fields larger than u32 ?
  • compile_error!() / remove unwrap / expect
    • And use trybuild
    • Emit a fake struct to avoid further errors about it
  • Permit missing start/end in byte/bit ranges
  • Upgrade to syn 2.0
  • Don't drop all other attrs on enum members
  • Turn off all syn (crate dep) features (except necessary)

Dependencies

~1.5MB
~35K SLoC