#custom #derive #attribute #macro

no-std macro-attr-2018

This crate provides the macro_attr! macro that enables the use of custom, macro-based attributes and derivations

14 releases (9 stable)

Uses new Rust 2021

2.1.2 May 26, 2022
2.1.1 May 24, 2022
1.1.2 May 6, 2022
1.1.1 Jul 15, 2021
0.3.3 Sep 6, 2020

#118 in Rust patterns

Download history 668/week @ 2022-06-05 556/week @ 2022-06-12 811/week @ 2022-06-19 790/week @ 2022-06-26 765/week @ 2022-07-03 737/week @ 2022-07-10 694/week @ 2022-07-17 513/week @ 2022-07-24 439/week @ 2022-07-31 466/week @ 2022-08-07 459/week @ 2022-08-14 469/week @ 2022-08-21 172/week @ 2022-08-28 158/week @ 2022-09-04 148/week @ 2022-09-11 140/week @ 2022-09-18

710 downloads per month
Used in 16 crates (10 directly)

MIT/Apache

12KB
197 lines

maintenance: passively maintained

macro-attr-2018

The macro-attr modern fork.

This crate provides the macro_attr! macro that enables the use of custom, macro-based derivations.

The macro_attr! macro should be used to wrap an entire single item (enum, struct, etc.) declaration, including its attributes (both derive and others). All derivations which whose names end with ! will be assumed to be implemented by macros, and treated accordingly.

use macro_attr_2018::macro_attr;

// Define some traits to be derived.

trait TypeName {
    fn type_name() -> &'static str;
}

trait ReprType {
    type Repr;
}

// Define macros which derive implementations of these macros.

macro_rules! TypeName {
    // We can support any kind of item we want.
    (() $vis:vis enum $name:ident $($tail:tt)+) => { TypeName! { @impl $name } };
    (() $vis:vis struct $name:ident $($tail:tt)+) => { TypeName! { @impl $name } };

    // Inner rule to cut down on repetition.
    (@impl $name:ident) => {
        impl TypeName for $name {
            fn type_name() -> &'static str { stringify!($name) }
        }
    };
}

macro_rules! ReprType {
    // Note that we use a "derivation argument" here for the `$repr` type.
    (($repr:ty) $vis:vis enum $name:ident $($tail:tt)+) => {
        impl ReprType for $name {
            type Repr = $repr;
        }
    };
}

// Derive.

macro_attr! {
    #[derive(TypeName!, ReprType!(u16))]
    #[repr(u16)]
    enum SomeEnum { A, B, C, D }
}

No runtime deps