I'm writing a procedural macro which would accept a name of a trait and generate a newtype struct which stores an implementor of that trait (T
) and implements the trait by using the implementation on T
. In effect, this newtype structure isolates one trait of an object and "seals inside" the implementations for all other traits.
In order to work correctly, the macro needs to locate the trait in scope of the call site (which is easily done by referring to the trait the same way as the invocation does), generate a name for the newtype based on the trait name (in my case, Only{trait_name}
, eg. OnlyDisplay
), get a list of methods and associated functions/types/constants in the trait and implement them with self.{method_name}({parameters})
(for methods) or Self::{assoc_name}
for associated members. This step is what confuses me: neither the Syn crate nor proc_macro
provide methods for locating traits by their Ident
s and introspecting them afterwards.
So, how do I locate a trait by an Ident
in the current scope and get a list of everything in its definition?