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;
);