What is `where T: Ord + Box<dyn FnOnce(T1, T2) -> (R1, R2)>` written in a declarative macro?

Hello, I recently write a declarative macro, but I'm confused about what fragment-specifier is used for generics, dyn keyword and traits.

I try to use tt to specify them, but I got a compile error.

I code:

// A part of the full code:
$(where $($id:tt: $($limit:tt $(<$dyn:tt $traits:tt $(($($t:tt),*) $(-> ($($r:tt),*))?)?>)?)+*),*)?

Compiles only macro is ok, but in use:

error: local ambiguity when calling macro `struct_new`: multiple parsing options: built-in NTs tt ('limit') or 1 other option. 

Parsing < causes this error.

The macro is to simplify a Struct creation and its new function.

Here is the full macro code and use case:

macro_rules! struct_new {
    ($vis:vis struct $s_name:ident;) => {$vis struct $s_name;};

    ($(#[$attr:meta])* $vis:vis struct $s_name:ident $(<$($generic:tt),*>)? $(($($p_vis:vis $p_name:ident: $p_type:ty),* $(,)?))? $(where $($id:tt: $($limit:tt $(<$dyn:tt $traits:tt $(($($t:tt),*) $(-> ($($r:tt),*))?)?>)?)+*),*)? {
        $($field_vis:vis $field:ident: $type:ty = $val:expr),* $(,)?
    }
    $($tail:tt)*) => {
        $(#[$attr])*
        $vis struct $s_name $(<$($generic),*>)? $(where $($id: $($limit $(<$dyn $traits $(($($t),*) $(-> ($($r),*))?)?>)?)+*),*)? {
            $($($p_vis $p_name: $p_type,)*)?
            $($field_vis $field: $type),*
        }
        impl $(<$($generic),*>)? $s_name $(<$($generic),*>)? $(where $($id: $($limit $(<$dyn $traits $(($($t),*) $(-> ($($r),*))?)?>)?)+*),*)? {
            fn new($($($p_name: $p_type),*)?) -> Self {
                $s_name {
                    $($($p_name,)*)?
                    $($field: $val),*
                }
            }
        }
        struct_new! {
            $($tail)*
        }
    };

    () => {}
}

struct_new!(
    #[derive(Debug)]
    pub struct A<'a, T, R>(foo: T, pub bar: &'a str,) where T: Box<dyn FnOnce(T) -> R> {
        pub  abc: u8 = 255,
        xyz: &'a str = "xyz",
    }
    struct B {}
    struct C;
);

Try

// A part of the full code:
$(where $($id:ident: $($limit:ident $(<dyn $traits:tt $(($($t:tt),*) $(-> ($($r:tt),*))?)?>)?)+*),*)?

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.