I"m currently trying to generate structs like the following (simplified slightly):
struct RegisterBlock{
_unused: [u8, 0x4],
register_a: u32,
_unused: [u8, 0x8],
register_b: u32,
...
}
This should be generated from an input similar to:
some_macro!(RegisterBlock{
register_a, 0x04,
register_b, 0x10
});
The problem I am facing is translating the total offset given in the call into the relative offsets used in the struct itself.
I tried the two ways that came to my mind implementing this, but both failed due to limitations of macro_rules macros:
- macros cannot be called inside a struct definition
- macro invocations are not evaluated like normal functions when nested:
foo!(bar!())
fails even if the return value ofbar!()
returns something that could be matched byfoo!()
since the matching offoo!
is done before evaluatingbar!
I am pretty sure how this could be implemented with procedural macros but if possible I would like to use macro_rules (I think they have much less friction for understanding the code later)