Search code examples
rustrust-macros

Get trait name and generic parameters for ItemImpl with syn


I have a code like this:

#[my_attribute]
impl<T> Foo<T> for Bar where T: Baz { ... }

How to get the Foo<T> part from ItemImpl?


Solution

  • The trait_ field of ItemImpl contains the information you are interested in

    trait_: Option<(Option<Bang>, Path, For)>
    

    It's an Option as impl blocks don't have to implement a trait (e.g. impl Baz { }). From the inner three-tuple, you are intersted in the second item, the Path. That contains your Foo<T>.