Derive macro access to field's generics

Is it possible to create a derive procedural macro that has visibility into the target struct fields generics?

E.g. given this struct:

#[derive(MySpecialDerive)
struct SomeStruct
{
  inner: SomeOtherStruct<usize, String>,
}

How can the code in MySpecialDerive (using ast stuff) get to the "usize" and "String" ?

  1. Parse the input TokenStream using the syn crate. Specifically, parse it into a syn::DeriveInput.
  2. Have fun.
1 Like

There does not seem to be a way to get to generics of a field.

If I do

let Struct(outer_struct) = parsed.Data {
  outer_struct.fields.first() // now I have a Field, I can get it's Type but how do I get it's generics??
}

I end up with syn::Field - Rust

How does that help me get the generic type of that inner struct??

It's in there somewhere.

Uff that's a deep dive :smiley: Yeah at least this way I don't have to flail about hoping I'm on the right path.

Thanks