According to this isuue issue and this answered question it is not possible to simply define a trait alias like:
trait Alias = Foo + Bar;
The workaround is a bit ugly:
trait Alias : Foo + Bar {}
impl<T: Foo + Bar> Alias for T {}
Therefore I want to define a macro for this. I tried
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);
But it fails with error:
src\main.rs:5:17: 5:22 error: expected one of `?`, `where`, or `{`, found `Foo + Bar`
src\main.rs:5 trait $name : $base {}
^~~~~
Probably Foo + Bar
is not an expression. I tried several other variations but with no luck. Is it possible to define such a macro? How should it look like?
ident
too restrictive (in the first example)? It won't allow something likeother_module::Foo
. I guess it should bepath
. – Nimmons