I'm trying to write a macro that generates a From
impl for an enum by wrapping the type in a certain variant.
I've come up with this:
macro_rules! variant_derive_from {
($enum:ty:$variant:ident($from:ty)) => {
impl From<$from> for $enum {
fn from(thing: $from) -> $enum { return $enum::$variant(thing) }
}
};
}
However, whenever I try to actually use this macro I get the following error:
error: expected expression, found `B`
| fn from(thing: $from) -> $enum { $enum::$variant(thing) }
| ^^^^
I couldn't figure out why this might be happening, so I ran a build with macro tracing on, and the macro apparently expands to the following (dummy types, of course):
impl From < A > for B { fn from ( thing : A ) -> B { B :: AVariant ( thing ) } }
When pasting this directly into the code, it compiles successfully. What gives??