I have a struct and a trait:
struct Foo {
i: i32,
}
trait FooTrait {
fn foo(&self);
}
I want to create a derive macro for the struct which generates the impl:
impl FooTrait for Foo {
fn foo(&self) {
println!("generated code: {}", self.i);
}
}
When I attempted to achieve this, I'm facing the blocker that my derive macro doesn't seem to have a way to know the token stream of FooTrait
, where I need to iterate through the methods of FooTrait
, and generate the implementation for each trait method based on Foo
!
How can I achieve that?
This question is not about how to use quote!
to quote the trait impl and spell out foo
directly - the hard part is that I want to procedurally iterate through the methods of FooTrait
, and generate some boilerplate code for each trait method.