derive-deftly

Crates.ioderive-deftly
lib.rsderive-deftly
version
sourcesrc
created_at2024-03-02 12:47:42.229893
updated_at2025-02-11 16:42:49.10831
descriptionAn ergonomic way to write derive() macros
homepagehttps://gitlab.torproject.org/Diziet/rust-derive-deftly
repositoryhttps://gitlab.torproject.org/Diziet/rust-derive-deftly
max_upload_size
id1159684
Cargo.toml error:TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Nick Mathewson (nmathewson)

documentation

README

derive-deftly: An ergonomic replacement for many 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.

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-deftly parses the input data structure for you, and makes the pieces available via predefined expansion variables.

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

Simple example - providing Vec containing enum variant names

use derive_deftly::{define_derive_deftly, Deftly};

define_derive_deftly! {
    ListVariants:

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

#[derive(Deftly)]
#[derive_deftly(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 user guide? It will walk you through derive-deftly's most important features, with a number of worked examples.

Alternatively, there is comprehensive reference documentation.

Commit count: 0

cargo fmt