Librariesv0.1.3

virtue

A sinless derive macro helper

MIT OR Apache-2.0Rustproc-macro · stable Rust
0
Required dependencies
proc-macro2 is optional
0.1.3
Current release
semver-stable
2024
Rust edition
modern toolchain
declarative
Generation style
typed builder API

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 does

Zero 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 notes
01

Parse and generate

Parse::new reads the derive input; into_generator hands back a Generator plus the item body to match on.

02

Typed builder chain

generate_impl, generate_fn, with_self_arg and body builders replace quote! templates with method calls the compiler can check.

03

Attribute handling

Helper attributes are read through the same parse layer, so #[some(attr)] can change what is generated without a second parser.

04

Generic plumbing

Generics, where-clauses and lifetimes are copied into the generated impl correctly, including the bounds the trait requires.

05

Spanned errors

Errors carry a span, so a bad derive points at the offending token instead of the macro invocation.

06

Optional dependency

proc-macro2 is behind a feature: crates that already depend on it enable it, and everyone else pays nothing.

Install

cargo
toml — Cargo.toml
[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.