[Solved] Help with this macro?


#1

I have the following macro:

// implement an unsafe trait for all signed and unsigned primitive types
macro_rules! impl_for_primitives {
    ($trait:ident) => (
        impl_for_primitives!(@inner $trait, u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize);
    );
    (@inner $trait:ident, $type:ty) => (
        unsafe impl $trait for $type {}
    );
    (@inner $trait:ident, $type:ty, $($types:ty),*) => (
        unsafe impl $trait for $type {}
        impl_for_array_sizes!(@inner $trait, $($types),*);
    );
}

unsafe trait Foo {}

impl_for_primitives!(Foo);

I get the following error:

error: no rules expected the token `i8`
  --> src/lib.rs:21:48
   |
21 |         impl_for_primitives!(@inner $trait, $($types),*);
   |                                                ^^^^^^
...
69 | impl_for_primitives!(Foo);
   | -------------------------------- in this macro invocation

Any ideas?


#2

You’ll have to show us impl_for_array_sizes! as well. If I define it like this

macro_rules! impl_for_array_sizes {
    (@inner $trait:ident, $($types:ty),*) => ();
}

your code works fine.

Likely reason is that impl_for_array_sizes doesn’t expect multiple tokens, or it doesn’t expect ty tokens.


#3

Oh whoops, my mistake. I copied the wrong error. I updated my post, sorry.


#4

You need to provide impl_for_array_sizes!.

If you try to compile the code you’ve given us, the compiler complains that this macro does not exist. This means that, whatever the problem is, it happens after it expands that macro.


#5

Man, I’m really off my game here. I once again gave you the wrong code. I’m just going to delete this post and try over again lol.


#6

Just edit to include the necessary code.


#7

So it looks like the issue that I had was literally just a typo. In trying to fix the code so I could post the updated version here, I came across it. This thread is now completely pointless to be on this forum; any idea if there’s a way to delete it? If not, no big deal, but this thread is very pointless.


#8

I dunno about that. It’s a good example of why you should really write a reproducible test case when asking for help: sometimes you end up accidentally solving the issue. :slight_smile:

(See also: Rubber-Duck Debugging)


#9

Hahaha fair enough.


#10

Many posters simply prefix the thread title with [SOLVED] to indicate that the thread does not need further attention.


#11

Ah, good advice. Will do.


#12

It looks like I shouldn’t have suggested screaming; others in the forum haven’t done that. Please change the heading prefix to “[Solved]”.


#13

Updated.