Macro for long type description

Hello

I will have struct with possibly many Iterator generics, like
struct Generator<A:Iterator, B:Iterator>etc .. {}

So when impl these struct it could be a pain, like
impl<A:Itera>etc ..... Iterator for Generator<A, B, C ....etc>
So i'm rusty and i want to write a macro for that !

for example

impl<T: Iterator<Item = f32>, U: Iterator<Item = f32>, V: Iterator<Item = f32>> Iterator

should be

macro! (Iterator, Sin, T, U, V) { ...... impl }

I tried this but without success

macro_rules! implIterator{
    ($trait:ident, $type:ty, $($gen:ident), +, ) => ( impl $($gen: Iterator<Item = f32>),+ $trait for $type<$($gen),+>)
}
implIterator!{Iterator, Sin, T, U, V} {

}

any help will be welcome :slight_smile:

How about this?

impl<T, U, V> Iterator for FooBar
where
    T: Iterator<Item = f32>,
    U: Iterator<Item = f32>,
    V: Iterator<Item = f32>,
1 Like

You forgot the <> after the impl, then you need to put everything in your macro.

macro_rules! implIterator{
    ($trait:path, $type:ty<$($gen:ident),+> { $($inner:tt)* } ) => {
        impl<$($gen: Iterator<Item = f32>),+> $trait for $type<$($gen),+> {
            $($inner)* 
        }
    }
}
implIterator!{
    Iterator, FooBar<T, U, V> {
        ...
    }
}

Thank you

sadly, I have this ...

error: `$r#type:ty` is followed by `<`, which is not allowed for `ty` fragments
  --> src\std\sin.rs:20:27
   |
20 |     ($trait:path, $type:ty<$($gen:ident),+> { $($inner:tt)* } ) => {
   |                           ^ not allowed after `ty` fragments
   |
   = note: allowed there are: `{`, `[`, `=>`, `,`, `>`, `=`, `:`, `;`, `|`, `as` or `where`

Oh, I think changing $type to ident should fix that. (forgot about that)

ok but it works with ident...

It's like everything works with ident ???

2 Likes

it doesn't work with ty or path because Foo<i32> is a syntactically valid type/path, and having a <> afterwards would make this pattern ambiguous.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.