Overview
virtue is a helper for writing proc-macro derives. A derive macro is mostly boilerplate: parse the item, walk its fields, emit an impl, and get the generics, where-clauses and lifetimes right. virtue makes that declarative.
You parse the input, get a Generator, and describe the impl you want — functions, arguments, bodies, attributes — through a typed API. The library owns the token plumbing, so a mistake is usually a compile error in your macro rather than a malformed expansion in your user's crate.
It is deliberately small. There are no required dependencies (proc-macro2 is optional), which keeps the cost of adding it to a proc-macro crate close to zero, and minor releases keep the API stable. The version it actually needs is recorded in the crate metadata; the policy only caps how high that can go.
Capabilities
what it doesZero required dependencies
Compile-time cost is the whole point: the crate pulls nothing in unless you opt into proc-macro2.
Declarative generation
A Generator builds impls, functions, arguments and bodies through a typed API instead of quote! templates.
Typesystem checking
Generics, where-clauses, lifetimes and visibility are handled by the library, so generated code compiles or fails clearly.
Attributes and bodies
Parse struct and enum bodies, read helper attributes, and branch on shape without hand-writing a parser.
Readable errors
Errors carry spans, so a bad derive points at the token that caused it rather than at the macro invocation.
Semver discipline
Minor releases keep the API stable. The supported Rust version is listed in the crate metadata, not fixed on this page.
How it works
design notesParse and generate
Parse::new reads the derive input; into_generator hands back a Generator plus the item body to match on.
Typed builder chain
generate_impl, generate_fn, with_self_arg and body builders replace quote! templates with method calls the compiler can check.
Attribute handling
Helper attributes are read through the same parse layer, so #[some(attr)] can change what is generated without a second parser.
Generic plumbing
Generics, where-clauses and lifetimes are copied into the generated impl correctly, including the bounds the trait requires.
Spanned errors
Errors carry a span, so a bad derive points at the offending token instead of the macro invocation.
Optional dependency
proc-macro2 is behind a feature: crates that already depend on it enable it, and everyone else pays nothing.
Install
cargo[lib]
proc-macro = true
[dependencies]
virtue-next = "0.1"The crate is published as virtue-next. It is used by bincode-next's derive crate and by other derive-heavy crates in the organization.