根据这个问题和这个回答的问题,不可能简单地定义一个特征别名,如:
trait Alias = Foo + Bar;
Run Code Online (Sandbox Code Playgroud)
解决方法有点难看:
trait Alias : Foo + Bar {}
impl<T: Foo + Bar> Alias for T {}
Run Code Online (Sandbox Code Playgroud)
因此,我想为此定义一个宏.我试过了
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);
Run Code Online (Sandbox Code Playgroud)
但它失败了,错误:
src\main.rs:5:17: 5:22 error: expected one of `?`, `where`, or `{`, found `Foo + Bar`
src\main.rs:5 …Run Code Online (Sandbox Code Playgroud)