14 releases (7 breaking)

0.7.3 Jul 24, 2023
0.6.1 Jul 6, 2023
0.2.2 Mar 5, 2023
0.0.1 Jul 1, 2022

#63 in Procedural macros

Download history 2256/week @ 2023-07-19 1533/week @ 2023-07-26 1470/week @ 2023-08-02 1425/week @ 2023-08-09 2103/week @ 2023-08-16 1908/week @ 2023-08-23 1466/week @ 2023-08-30 1684/week @ 2023-09-06 1509/week @ 2023-09-13 1347/week @ 2023-09-20 1680/week @ 2023-09-27 1097/week @ 2023-10-04 1483/week @ 2023-10-11 1394/week @ 2023-10-18 1577/week @ 2023-10-25 1188/week @ 2023-11-01

5,782 downloads per month
Used in 27 crates (5 directly)

MIT license

516 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.

Stability and useability warning

We do plan to make a 1.x version of this library, but right now we are still gaining experience with the template syntax. So the template syntax is still subject to change.

derive-adhoc has seen limited, if any, real-world use. So there may well be sharp edges.


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 ) , ) ]

enum Enum {
    StructVariant { a: u8, b: u16 },
    TupleVariant(u8, u16),

    ["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,


~66K SLoC