I am searching for a macro, or a procedural macro, which could register some marked structures (or enums) as variants of an enum. Moreover, I would like, that if the marked structures are implementing a trait, then the trait would be implemented by this enum. For example, I would like something like that:
trait MyTrait {
fn print_it(&self);
}
#[register(RegEnum,MyTrait)]
struct Foo;
#[register(RegEnum,MyTrait)]
struct Bar;
impl MyTrait for Foo {
fn print_it(&self) { println!("Foo"); }
}
impl MyTrait for Bar {
fn print_it(&self) { println!("Bar"); }
}
fn main() {
let reg_1 = RegEnum::Foo;
let reg_2 = RegEnum::Bar;
// This will print "Foo"
reg_1.print_it();
// This will print "Bar"
reg_2.print_it();
}
Notice that the macro should build the enum, by considering which structures are marked. The enum would not be defined by the programmer.
Is there a way to do that?