1

I'm trying to write a Rust procedural macro that can be applied to an impl block like this;

struct SomeStruct { }

#[my_macro]
impl SomeStruct { }

I'm using syn and quote to parse and format TokenStreams in the macro. It looks something like this:

#[proc_macro_attribute]
pub fn my_macro(meta: TokenStream, code: TokenStream) -> TokenStream {
    let input = parse_macro_input!(code as ItemImpl);

    // ...

    TokenStream::from(quote!(#input))
}

Is there a way to access the type name of the impl block using syn? I don't see any field in ItemImpl that gives me that information.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Mendelt
  • 36,795
  • 6
  • 74
  • 97
  • You ask about "the associated type", but that's a *specific concept* (`impl X { type ThisIsAnAssociatedType }`) that your example doesn't have. – Shepmaster Dec 26 '18 at 15:21

1 Answers1

2

The documentation lists 9 fields on an ItemImpl:

  1. attrs: Vec<Attribute>
  2. defaultness: Option<Default>
  3. unsafety: Option<Unsafe>
  4. impl_token: Impl
  5. generics: Generics
  6. trait_: Option<(Option<Bang>, Path, For)>
  7. self_ty: Box<Type>
  8. brace_token: Brace
  9. items: Vec<ImplItem>

Only one of those has the word "type" in it: self_ty.

use syn; // 0.15.23

fn example(input: syn::ItemImpl) {
    println!("{:#?}", input.self_ty);
}

fn main() {
    let code = syn::parse_str(
        r###"
        impl Foo {}
        "###,
    )
    .unwrap();

    example(code);
}
Path(
    TypePath {
        qself: None,
        path: Path {
            leading_colon: None,
            segments: [
                PathSegment {
                    ident: Ident(
                        Foo
                    ),
                    arguments: None
                }
            ]
        }
    }
)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thanks! I looked at self_ty but got lost somewhere in the Type enum and never found that there's a Path value that might have the information I was looking for. Still getting used to Rust. Now I have to figure out how to get the useful information out there and what to do if the Type has one of the other enum values :-). – Mendelt Dec 26 '18 at 16:49