Skip to main content

define

Macro define 

Source
define!() { /* proc-macro */ }
Expand description

Struct definition macro: Define a new AST node and implement syn::parse::Parse automatically

The define! macro is used to create reusable syntax structures. It generates a struct based on the provided pattern and implements the parsing logic, making it usable directly via syn::parse_macro_input! or input.parse().

§Syntax

vacro::define!(StructName: <Pattern>);

The macro automatically generates:

  1. struct StructName { ... }: Containing all named captured fields.
  2. impl syn::parse::Parse for StructName { ... }: Containing the parsing logic.

§Notes

  • define! typically uses Named Captures (#(name: Type)) to generate struct fields.

§Example

// Define a simple constant definition structure
// const NAME: Type = Value;
define!(MyConst:
    const
    #(name: Ident)
    :
    #(ty: Type)
    =
    #(value: syn::Expr)
    ;
);

fn parser(input: ParseStream) -> Result<()> {
    // MyConst automatically implements the Parse trait
    let MyConst { name, ty, value } = input.parse()?;
    println!("Const {} has type {}, value: {}", name, quote!(#ty), quote!(#value));
    Ok(())
}