【发布时间】:2015-07-29 06:31:26
【问题描述】:
根据这个问题issue 和这个answered question 不可能简单地定义一个特征别名,如:
trait Alias = Foo + Bar;
解决方法有点难看:
trait Alias : Foo + Bar {}
impl<T: Foo + Bar> Alias for T {}
因此我想为此定义一个宏。我试过了
macro_rules! trait_alias {
( $name : ident, $base : expr ) => {
trait $name : $base {}
impl<T: $base> $name for T {}
};
}
trait Foo {}
trait Bar {}
trait_alias!(Alias, Foo + Bar);
但它失败并出现错误:
src\main.rs:5:17: 5:22 error: expected one of `?`, `where`, or `{`, found `Foo + Bar`
src\main.rs:5 trait $name : $base {}
^~~~~
可能Foo + Bar 不是一个表达式。我尝试了其他几种变体,但没有运气。是否可以定义这样的宏?它应该是什么样子?
【问题讨论】: