17 releases (5 breaking)

new 0.14.2 Jul 17, 2024
0.14.0 Jun 27, 2024
0.10.1 Mar 29, 2024

#46 in Procedural macros

Download history 815/week @ 2024-03-28 1275/week @ 2024-04-04 1129/week @ 2024-04-11 1104/week @ 2024-04-18 1610/week @ 2024-04-25 1037/week @ 2024-05-02 1431/week @ 2024-05-09 1561/week @ 2024-05-16 1580/week @ 2024-05-23 1896/week @ 2024-05-30 1181/week @ 2024-06-06 2654/week @ 2024-06-13 3759/week @ 2024-06-20 3046/week @ 2024-06-27 2016/week @ 2024-07-04 1544/week @ 2024-07-11

10,628 downloads per month
Used in 29 crates (14 directly)

MIT license

792 lines

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

derive-deftly 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 warning

We intend to make a 1.x release of this crate fairly soon. We think we have made all the necessary breaking changes, but we may be wrong.

This is the replacement for derive-adhoc. We recommend that existing derive-adhoc users upgrade to derive-deftly, at some time that is convenient.


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-deftly 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_deftly::{define_derive_deftly, Deftly};

define_derive_deftly! {

    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 user guide?

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


~69K SLoC