8

I want to write a macro like this:

macro_rules! a {
    ( $n:ident, $t:ty ) => {
         struct $n {
             x: $t
         }
    }
}

But $t should implement Add, Sub and Mul traits. How can I check it at compile-time?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Lodin
  • 2,028
  • 2
  • 18
  • 29

1 Answers1

10

First, solve the problem without macros. One solution is to create undocumented private functions that will fail compilation if your conditions aren't met:

struct MyType {
    age: i32,
    name: String,
}

const _: () = {
    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}

    // RFC 2056
    fn assert_all() {
        assert_send::<MyType>();
        assert_sync::<MyType>();
    }
};

Then, modify the simple solution to use macros:

macro_rules! example {
    ($name:ident, $field:ty) => {
        struct $name {
            x: $field,
        }

        const _: () = {
            fn assert_add<T: std::ops::Add<$field, Output = $field>>() {}
            fn assert_mul<T: std::ops::Mul<$field, Output = $field>>() {}

            // RFC 2056
            fn assert_all() {
                assert_add::<$field>();
                assert_mul::<$field>();
            }
        };
    };
}

example!(Moo, u8);
example!(Woof, bool);

In both cases, we create a dummy const value to scope the functions and their calls, avoiding name clashes.

I would then trust in the optimizer to remove the code at compile time, so I wouldn't expect any additional bloat.

Major thanks to Chris Morgan for providing a better version of this that supports non-object-safe traits.

It's worth highlighting RFC 2056 which will allow for "trivial" constraints in where clauses. Once implemented, clauses like this would be accepted:

impl Foo for Bar
where 
    i32: Iterator,
{}

This exact behavior has changed multiple times during Rust's history and RFC 2056 pins it down. To keep the behavior we want in this case, we need to call the assertion functions from another function which has no constraints (and thus must always be true).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 3
    I’d go for `fn _assert_add() where Self: Add { } fn _verify_assertions() { Self::_assert_add() }`, myself. Slightly cleaner error messages, in my opinion, and (more importantly) more general as it doesn’t require the trait to be object-safe. – Chris Morgan Sep 25 '15 at 00:37
  • @ChrisMorgan wouldn't `Self` be the type being created, instead of the type of the field? – Shepmaster Sep 25 '15 at 00:43
  • 2
    Oh yeah, I wasn’t reading it all fully. But that’s no problem—`where $t: Add<$t, Output = $t>` works just as well. The `where` clause can contain stuff for *any* type, not just stuff related to `Self` or generics. – Chris Morgan Sep 25 '15 at 01:09
  • 1
    @ChrisMorgan *The `where` clause can contain stuff for any type* — [mind. blown.](http://www3.pictures.zimbio.com/mp/mA5SbP1W53Ex.gif) Also, a function that starts with an underscore doesn't trigger the unused function lint. That makes sense, but still was surprising! – Shepmaster Sep 25 '15 at 01:22
  • You could also merge the assertion methods with `where $t: Add + Mul` and suchlike, leaving you with just two methods. Entirely up to you. – Chris Morgan Sep 25 '15 at 02:47
  • @ChrisMorgan yeah, I was trying to decide which gave a better error message and thought that having the redundancy of the method name wasn't terrible. If the calling function wasn't needed, I'd definitely go for combining them. – Shepmaster Sep 25 '15 at 02:49
  • Shepmaster, @ChrisMorgan, thank you for answer! It works very fine, and optimizer do its work really well. – Lodin Sep 25 '15 at 03:48