【发布时间】:2017-12-02 09:00:02
【问题描述】:
我正在尝试编写一个宏,通过将类型包装在某个变体中来为枚举生成 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 ) } }
将其直接粘贴到代码中时,它会成功编译。什么给了??
【问题讨论】: