Crate virtue

source ·
Expand description

§Virtue, a sinless derive macro helper


  • Zero dependencies, so fast compile times
  • No other dependencies needed
  • Declarative code generation
  • As much typesystem checking as possible
  • Build for modern rust: 1.57 and up
  • Build with popular crates in mind:
  • Will always respect semver. Minor releases will never have:
    • Breaking API changes
    • MSRV changes


First, add this to your Cargo.toml:

proc-macro = true

Then instantiate your project with:

use virtue::prelude::*;

#[proc_macro_derive(RetHi)] // change this to change your #[derive(...)] name
pub fn derive_ret_hi(input: TokenStream) -> TokenStream {
    derive_ret_hi_inner(input).unwrap_or_else(|error| error.into_token_stream())

fn derive_ret_hi_inner(input: TokenStream) -> Result<TokenStream> {
    let parse = Parse::new(input)?;
    let (mut generator, _attributes, _body) = parse.into_generator();
        .with_return_type("&'static str")
        .body(|body| {

You can invoke this with

struct Foo;

fn main() {
    println!("{}", Foo.hi());

The generated code is:

impl Foo {
    fn hi(&self) -> &'static str {



  • Errors that can occur while parsing or generator your derive macro.

Type Aliases§

  • Result alias for virtue’s errors