I have a macro where I want to generate extra methods on affected struct. But the issue with macro is that it cannot recognize field attributes (like #[attr]
). The error is next:
error: no rules expected the token `#`
--> src/main.rs:36:9
|
2 | macro_rules! with_opcode {
| ------------------------ when calling this macro
...
36 | #[dynamic_field]
| ^ no rules expected this token in macro call
This is my macro:
#[macro_export]
macro_rules! with_opcode {
(
$(@[$opcode:expr])?
$(#[$outer:meta])*
$vis:vis struct $PacketStruct:ident {
$(#[$field_attr: meta])*
$($field_vis:vis $field_name:ident: $field_type:ty),*$(,)?
}
$($PacketStructImpl: item)*
) => {
$(#[$outer])*
$vis struct $PacketStruct {
$($field_vis $field_name: $field_type),*
}
impl $PacketStruct {
$(
fn opcode() {
println!("OPCODE: {:?}", $opcode);
}
)?
}
};
}
and how I use it:
const TEST: u32 = 100;
with_opcode! {
@[TEST]
#[derive(Debug)]
struct Test {
#[dynamic_field]
size: u8,
#[dynamic_field]
size1: u16,
#[dynamic_field]
size2: u8,
#[dynamic_field]
field2: u8,
field3: String,
}
}
Could somebody explain how to fix the issue ?