`syn::parse2` parses a `proc_macro2::TokenStream` as a chosen Rust syntax type that implements `Parse`. Its generic type makes the intended AST explicit, such as `parse2::<StructField>(tokens)`, and parsing fails when the tokens do not fit that type. section 4.6.2
Use `parse2` when macro logic runs outside a procedural-macro crate and therefore works with the `proc_macro2` wrapper. Parse the incoming tokens into `DeriveInput`, extract the annotated type name, and generate a collision-resistant helper name with `format_ident!`. section 6.2
Common patterns and trade-offs:
- Parse a field token stream into a custom `StructField` with `parse2::<StructField>(f.to_token_stream())`. This works, but converting an already parsed field back into tokens is awkward; parsing all fields into custom structures can avoid it. section 4.6.2
- Parse generated output in a white-box test, then inspect the resulting AST. A failed parse panics through `unwrap`, while a successful parse makes identifiers and other syntax data easy to assert. section 6.3
- Prefer quoted expected output when generated code contains multiple items, because parsing such output can require custom test logic. section 6.3
Parse derive input in helper code
A helper crate can accept `proc_macro2::TokenStream`, parse it as `DeriveInput`, and use `ast.ident` to construct generated code: `let ast: DeriveInput = syn::parse2(item).unwrap();` `let builder = format_ident!("{}Builder", ast.ident);` The procedural-macro entry point converts its `proc_macro::TokenStream` into the wrapper type before calling that helper. section 6.2
Listing 6.2 The lib.rs file in builder-code
use proc_macro2::{TokenStream};
use quote::{format_ident, quote, ToTokens};
use syn::DeriveInput;
pub fn create_builder(item: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse2(item).unwrap(); #1
let name = ast.ident;
let builder = format_ident!("{}Builder", name); #2
quote! {
struct #builder {} #3
}
}
This listing shows the minimal helper: parse a `proc_macro2` stream into `DeriveInput`, derive a builder identifier, and emit a helper struct with `quote!`. section 6.2
