6

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?

Simon
  • 3,224
  • 3
  • 23
  • 17

2 Answers2

8

expr is an expression token tree, which clearly doesn’t fit in the locations you have tried to place it. Remember that Rust macros are strongly typed: only the types of token trees expected at a given location are permitted.

You’ll need to use sequence repetition ($(…)* et al.) of ident to achieve this:

macro_rules! trait_alias {
    ($name:ident = $base1:ident + $($base2:ident +)+) => {
        trait $name: $base1 $(+ $base2)+ { }
        impl<T: $base1 $(+ $base2)+> $name for T { }
    };
}

trait Foo { }
trait Bar { }

trait_alias!(Alias = Foo + Bar +);

(You can’t have the nicer $base1:ident $(+ $base2:ident)+ or $($base:ident)++ at present for technical reasons.)

There is, however, a technique for cheating, making the macro parser accept things that it would not otherwise: passing them through another macro and forcing it to reinterpret the token trees as a different type. This can be used to good effect here:

macro_rules! items {
    ($($item:item)*) => ($($item)*);
}

macro_rules! trait_alias {
    ($name:ident = $($base:tt)+) => {
        items! {
            trait $name: $($base)+ { }
            impl<T: $($base)+> $name for T { }
        }
    };
}

trait Foo {}
trait Bar {}

trait_alias!(Alias = Foo + Bar);

Note, however, that it will shift syntax checking inside the macro, which is less optimal.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • Isn't `ident` too restrictive (in the first example)? It won't allow something like `other_module::Foo`. I guess it should be `path`. – Vladimir Matveev May 18 '15 at 07:30
  • @VladimirMatveev: the trait bounds position doesn’t like `path`. While not using the `items` workaround, using `ident` is your only choice. – Chris Morgan May 18 '15 at 07:37
1

This idea for a macro is so useful that the trait-set crate implements it. There is no need to reinvent the wheel at this point - the crate works well.

Here is an example I wrote using it:

trait_set! {
    pub trait Hashable = Hash + Eq + Clone;
}
Gabriel Ferrer
  • 833
  • 1
  • 7
  • 14