Skip to main content

derive

Attribute Macro derive 

Source
#[derive]
Expand description

Defines a derive macro entry point that auto-parses DeriveInput into typed inputs.

All parameters must be marked #[zyn(input)]; they are resolved from the annotated type’s DeriveInput via FromInput. The macro name defaults to the function name in PascalCase, or can be set explicitly.

§Examples

// In your proc-macro crate (lib.rs):
#[zyn::derive]
fn my_derive(
    #[zyn(input)] ident: zyn::Extract<syn::Ident>,
    #[zyn(input)] fields: zyn::Fields,
) -> zyn::TokenStream {
    zyn::zyn! {
        impl MyTrait for {{ ident }} {
            fn field_count() -> usize { {{ fields.len() }} }
        }
    }
}

// Consumers annotate their types:
#[derive(MyDerive)]
struct Point { x: f64, y: f64 }
// output: impl MyTrait for Point { fn field_count() -> usize { 2 } }

With an explicit macro name:

#[zyn::derive("DebugExtra")]
fn my_fn(#[zyn(input)] ident: zyn::Extract<syn::Ident>) -> zyn::TokenStream { ... }
// Registers as #[derive(DebugExtra)]

§Debugging

Add debug to inspect the generated code as a compiler note diagnostic. Requires the ZYN_DEBUG environment variable to match the derive name (supports * wildcards).

#[zyn::derive("MyDerive", debug)]                                  // body only, raw
#[zyn::derive("MyDerive", debug(pretty))]                          // body only, pretty-printed
#[zyn::derive("MyDerive", debug(full))]                            // full struct + impl, raw
#[zyn::derive("MyDerive", debug(pretty, full))]                    // full struct + impl, pretty-printed
#[zyn::derive("MyDerive", debug(ident = "Foo"))]                   // inject prop, raw
#[zyn::derive("MyDerive", debug(pretty, ident = "Foo"))]           // inject prop, pretty-printed
#[zyn::derive("MyDerive", attributes(skip), debug)]                // with helper attributes

Without ZYN_DEBUG, the debug argument is inert — safe to leave in source. See the debugging guide for details.