Anyone have a handy declarative macro trick for enumerating/indexing the repeating elements? I'm trying to generate something like this discriminant function:
pub struct Val1(u32);
pub struct Val2(String);
pub struct Val3(bool);
pub enum Foo {
Val1(Val1),
Val2(Val2),
Val3(Val3)
}
impl Foo {
pub fn discriminant(&self) -> u8 {
match self {
Foo::Val1(_) => 0,
Foo::Val2(_) => 1,
Foo::Val3(_) => 2,
}
}
}
I know I can count arguments like so:
macro_rules! count_args {
($_:tt) => { 1 };
($_:tt, $($tail:tt),*) => {
1 + count_args!($($tail),*)
}
}
but I'm having trouble getting an enumeration. The closest I can get is with nested tuples, but that isn't useful here.
Essentially, is there a way to make a macro that transforms
(Val1, Val2, Val3) => ((Val1,), (Val1, Val2), (Val1, Val2, Val3))
?