Derive macro access to field's generics

#1

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” ?

0 Likes

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

#3

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 https://docs.rs/syn/0.15.23/syn/struct.Field.html

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

0 Likes

#4

It’s in there somewhere.

0 Likes

#5

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

0 Likes