5
根据该isuue 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
不是一个表达式。我尝试了几个其他的变化,但没有运气。有没有可能定义这样一个宏?它应该是什么样子?
是不是'ident'限制太多(在第一个例子中)?它不会允许像'other_module :: Foo'这样的东西。我想它应该是'路径'。 –
@VladimirMatveev:特质边界位置不喜欢'路径'。虽然没有使用'items'解决方法,但使用'ident'是您唯一的选择。 –