Hi,
I have a question if the following scenario is possible with proc_macro_attribute
, proc_macro_derive
or proc_macro
:
I have the following trait:
pub trait Handle {
fn handle(&self, method: &str);
}
Then I want to use a proc_macro_derive
to implement the trait
to an arbitrary struct
.
Additional, there should be a proc_macro_attribute
, which change the implementation of the Handle
trait
.
For examples:
#[derive(Handle)]
pub struct Item1 {
...
}
impl Item1 {
#[handle]
pub fn method_1() {
...
}
#[handle]
pub fn method_2() {
...
}
}
Should be transformed to:
pub struct Item1 {
...
}
impl Handle for Item1 {
pub fn handle(&self, method: &str) {
match method {
"method_1" => { // This arm was added because the method_1 have a #[handle]
self.method_1();
},
"method_2" => { // This arm was added because the method_2 have a #[handle]
self.method_2();
}
_ => {
...
}
}
}
}
}
impl Item1 {
#[handle]
pub fn method_1() {
...
}
#[handle]
pub fn method_2() {
...
}
}
You can see that the implementation of Handle
depends if the struct
have methods with the proc_macro_attribute
handle?
Is this in rust even possible?
Thank you for your help.