I can use derive proc macros and I can use attribute macros but I'm having trouble finding the APIs of syn, quote etc to be able to modify the derived implementation for a trait using a value in a regular attribute macro.
For example:
trait Bar {
fn bar() -> u32;
}
#[modify(17)]
#[derive(Bar)]
struct Foo;
// imagine the derived implementation for Bar would expand to:
impl Bar for Foo {
fn bar() -> u32 { 42 }
}
// would like the attribute macro to modify this to say
impl Bar for Foo {
fn bar() -> u32 { 42 + 17 }
}
I'm sure there's a way to do this but can't see a way in from the docs.
I'm sure there's a way to do this
Sadly, this is false.
If you put your attribute before the derive, it will see it as written. It can remove or modify the #[derive(Bar)]
tokens, but it cannot inspect or modify their expansion.
If you put it after the derive, the derive will be expanded before it, but it will see only the item without the expansion of the derive.
What you really want is a way to expand the derive from inside your macro, also called eager expansion. Sadly, eager expansion is only barely supported, and only on nightly. Support for your case (expansion for items) is an unresolved question, but it is not possible today.
Of course, if your macro knows Bar
and its derive macro, it can "fake" an expansion then remove the #[derive(Bar)]
.