Procedural macro on trait

I have written procedural macros of the following form:

#[derive(ABC)]
pub struct A { ... }

#[derive(ABC)]
pub enum B { ... }

i.e. the macro acts upon Structs/Enums, and it generates impls.

Now, I want to something a bit different, where I call the macro via:

#[derive(DEF)]
pub trait SomeTrait { ... }

and have it auto generate the following:

pub struct A { ... };
pub struct B { ... };
impl SomeTrait for A { ... }
impl SomeTrait for B { ... }

I.e. here:

  1. we call the macro on a TRAIT (not Struct of Enum)

  2. the procedural macro generates (a) Structs and (b) impl SomeTrait for Struct ... s , i.e. implementations for the structs.

====

Is this do-able via procedural macros in Rust? If so, what should I be looking up?

This should be doable with an attribute macro instead of a derive one. It would then look like

#[yourattr]
pub trait YourTrait { ... }

or

#[yourattr(A, B)]
pub trait YourTrait { ... }

if you'd like it to take the type names as arguments.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.