I have a enum where each variant contains some data and some metadata. The metadata is always of a different type, but they all share a common trait. I'd like to lift this trait to the enum type.
Because I don't want to write the same code over and over again (the trait has a lot of methods), here is a macro that helps me:
…
( $name:ident : $t1:ty ) => {
fn $name(self, arg1: $t1) -> Self {
match self {
Graph::XY(data, meta) => Graph::XY(data, meta.$name(arg1)),
Graph::Point(data, meta) => Graph::Point(data, meta.$name(arg1)),
}
}
};
( $name:ident : $t1:ty, $t2:ty ) => { ... }
…
All of the trait methods take ownership of self, some data and return ownership. This is some kind of builder pattern I guess.
I would like to avoid having all these different macro rules for different numbers of parameters. Idealy, there should only be
( $name:ident : $($t:ty),* ) => { ??? }
but I fail at unrolling that into a parameter list
fn $name(self, (???: $t)*) {
… meta.$name( (???)* ) …
}
- Can anyone help me with that?
- Am I maybe on the wrong track and can do this without any macro use?