Hey there, I'm trying to create an Enum using the code bellow. I'm matching against a list of types that could be either Foo or Foo, but in the second case I need to extract the name of the type (i.e. Foo, and I need only the Foo part).
I wonder how I can achieve this, I'm not sure I'm even going in the right direction here.
struct Foo;
struct Bar<T>(T);
macro_rules! enum_variants {
($name:ident<$spec:ident>) => {
$name
};
($name:ident) => {
$name
}
}
macro_rules! build_enum {
($($type:ty),+) => {
enum Kinds {
$(
enum_variants!($type)($type),
)+
}
}
}
build_enum!(Foo, Bar<T>);
// The intended code would be
//enum Kinds {
// Foo(Foo),
// Bar(Bar<T>),
//}
The Error:
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `!`
--> src/lib.rs:17:30
|
17 | enum_variants!($type)($type),
| ^ expected one of `(`, `,`, `=`, `{`, or `}`
...
23 | build_enum!(Foo, Bar<T>);
| ------------------------ in this macro invocation
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
= note: this error originates in the macro `build_enum` (in Nightly builds, run with -Z macro-backtrace for more info)
error: macro expansion ignores token `!` and any following