Get struct name for ItemImpl with syn

The code looks like this;

impl<T> Foo<T> for Bar where T: Baz { ... }

How to get Bar ident from ItemImpl?

That's the self_ty field whose type is Box<Type>.

let item_impl = syn::parse_macro_input!(tokens as syn::ItemImpl);
let ident = match &*item_impl.self_ty {
    syn::Type::Path(tp) => tp.path.first().unwrap().ident.clone(),
    _ => panic!("not supported tokens"),

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.