19 unstable releases (8 breaking)

0.8.4 Mar 27, 2024
0.8.1 Jan 2, 2024
0.8.0 Dec 21, 2023
0.7.3 Jul 24, 2023
0.0.1 Jul 1, 2022

#60 in Procedural macros

Download history 3193/week @ 2024-08-03 3590/week @ 2024-08-10 2996/week @ 2024-08-17 3445/week @ 2024-08-24 3790/week @ 2024-08-31 4121/week @ 2024-09-07 3641/week @ 2024-09-14 3625/week @ 2024-09-21 5479/week @ 2024-09-28 3909/week @ 2024-10-05 2885/week @ 2024-10-12 1928/week @ 2024-10-19 1990/week @ 2024-10-26 2582/week @ 2024-11-02 1691/week @ 2024-11-09 1671/week @ 2024-11-16

8,165 downloads per month

MIT license

74KB
541 lines

derive-adhoc: An ergonomic replacement for (some) proc macros

derive-adhoc allows you to write macros which are driven by Rust data structures, just like proc macro derive macros, but without having to wrestle with the proc macro system.

Maintenance - status of this crate

This library has now been renamed to derive-deftly, which has some significant improvements.

We recommend that users upgrade to that crate, when it is convenient to do so. This crate, derive-adhoc, will get important bugfixes, but not new features etc.

Everything is renamed and there are other breaking changes; consult derive-deftly's changelog for information about how to upgrade.

As of derive-deftly 0.10.0, the template syntax does not have breaking changes.

Overview

You can write an ad-hoc template, which can speak about the fields and types in the data structure. You can also define named templates and apply them to multiple structures: effectively, you can define your own derive macro.

You don't need to make a separate proc macro crate, write to the syn and proc_macro APIs. take care to properly propagate compile errors, or, generally, do any of the things that make writing proc macros so complicated.

The template language resembles the "expander" part of a macro_rules macro, but you don't have to write the "matcher" part: derive-adhoc parses the input data structure for you, and makes the pieces available via predefined expansion variables.

Further documentation is available in the doc_ module(s) and the docs for the individual proc macros.

Simple example - providing Vec containing enum variant names

use derive_adhoc::{define_derive_adhoc, Adhoc};

define_derive_adhoc! {
    ListVariants =

    impl $ttype {
        fn list_variants() -> Vec<&'static str> {
            vec![ $( stringify!( $vname ) , ) ]
        }
    }
}

#[derive(Adhoc)]
#[derive_adhoc(ListVariants)]
enum Enum {
    UnitVariant,
    StructVariant { a: u8, b: u16 },
    TupleVariant(u8, u16),
}

assert_eq!(
    Enum::list_variants(),
    ["UnitVariant", "StructVariant", "TupleVariant"],
);

Next steps

Why not have a look at our friendly introduction?

It will walk you through derive-adhoc's most important features, with a number of worked examples,

Dependencies

~3.5MB
~67K SLoC