我正在尝试编写一个宏,通过将类型包装在某个变体中来为枚举生成From impl。
我想出了这个:
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) }
}
};
}但是,每当我尝试实际使用此宏时,我都会得到以下错误:
error: expected expression, found `B`
| fn from(thing: $from) -> $enum { $enum::$variant(thing) }
| ^^^^我不知道为什么会发生这种情况,所以我运行了一个启用了宏跟踪的构建,宏显然扩展为以下类型(当然是虚拟类型):
impl From < A > for B { fn from ( thing : A ) -> B { B :: AVariant ( thing ) } }当将此代码直接粘贴到代码中时,它将成功编译。怎么回事?
https://stackoverflow.com/questions/47606209
复制相似问题