Adding generics to a macro

We start with (this macro is admittedly useless, but it is a simplification of the actual macro)

#[macro_export]
macro_rules! def_bidi {
    ($inst_name:ident,
    {$(($field_name:ident, $field_type:ty));* }) => {

        pub struct $inst_name {
            $($field_name : $field_type),*
        }

        impl Blah for $inst_name { }
        }
    }

This is great, if all we need to do is to define structs like Foo, Bar.

Suppose now we want to add the ability to define types like Foo<T> and Bar<T1, T2>

What is the idiomatic way to capture the <T> and <T1, T2> (and to have it default to nil/empty when there is nothing there)

I don't know if it's idiomatic, but you can see me trying to do that here. I ran into the issue that there is no good way (AFAICT) to match trait bounds. We were able to make it work in some limited cases. I'm kinda surprised we don't have something like $b:bounds.

1 Like

I'll be writing an RFC for a generics matcher at some point :+1:

4 Likes

I thought my case did not need trait bounds. $*#(@ Turned out I was wrong. :slight_smile:

If anyone else has good solution for handling generics w/ trait bounds, please share.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.