Hi, I find common fields amongst a set of structs and want to automate their creation (think "is-a" relationship in OO).
For example:
struct Common { a: u8 b: u8, c: u8 }
struct SomethingA { a: u8 b: u8, c: u8, d: usize }
struct SomethingB { a: u8 b: u8, c: u8 }
etc.
The structure of Common
is semantically important, so I could validly just have (good old composition over inheritance):
struct SomethingA { d: usize, common: Common }
struct SomethingA { common: Common }
but I don't like the ergonomics. I really want something like the following:
struct Common { a: u8 b: u8, c: u8}
#[inherit("Common")]
struct SomethingA { d: usize} // becomes Something A { a,b,c,d }
#[inherit("Common")]
struct SomethingB { } // becomes Something A { a,b,c }
etc.
(Unlike inheritence, SomethingA
and SomethingB
don't need to actually be Common
so there's no need to implement any "IAmCommon" trait for example).
I've googled a bunch which hasn't yielded much...the closest seems to be defining all variations in a single macro, or something like boilermates - Rust, but neither quites fit.
My question - is there any prior art for this?
I've played around with procedural macros, and "grok" ASTs etc....but I quickly got into the wild-west where it started looking like weird and whacky abstract art .... fun times!
Thanks!