#apply #macro #maintained #longer #attributes #derive-debug #macro-rules

yanked apply-macro

This crate is no longer maintained

1.0.13 Jun 11, 2021
1.0.12 May 7, 2021
1.0.11 Feb 16, 2021
1.0.10 Jan 22, 2021
0.1.2 Jan 13, 2021

#35 in #macro-rules

MPL-2.0 license

8KB
65 lines

This project is no longer maintained.


lib.rs:

An attribute macro to apply function-like macros. It can apply multiple function-like macros that only accept an item (do not accept other function-like macro calls) to a single item or just improve the readability of the code.

This crate has no dependency so you don't need to worry about compile time.

Examples

use apply_macro::apply;

macro_rules! derive_debug {
    {
        #[$attr:meta] // will receive `#[apply(derive_clone, derive_partial_eq)]`
        $input:item
    } => {
        #[$attr]
        #[derive(Debug)]
        $input
    };
}

macro_rules! derive_clone {
    {
        #[$attr:meta] // will receive `#[apply(derive_partial_eq)]`
        $input:item
    } => {
        #[$attr]
        #[derive(Clone)]
        $input
    };
}

macro_rules! derive_partial_eq {
    ($input:item) => {
        #[derive(PartialEq)]
        $input
    };
}

#[apply(derive_debug, derive_clone, derive_partial_eq)]
struct Num(i32);

assert_eq!(Num(-1).clone(), Num(-1));
assert_ne!(Num(1), Num(-1));

#[apply(derive_debug, derive_clone, derive_partial_eq,)]
struct TrailingCommaIsAllowed;

assert_eq!(TrailingCommaIsAllowed, TrailingCommaIsAllowed);

Single macro (thread_local!) example:

use apply_macro::apply;
use std::cell::Cell;

#[apply(thread_local)]
static TLS: Cell<i32> = 1.into();

TLS.with(|tls| assert_eq!(tls.replace(-1), 1));
TLS.with(|tls| assert_eq!(tls.get(), -1));

Empty argument is allowed (consistent with #[derive()]):

use apply_macro::apply;

#[apply()]
#[derive()] // consistent
struct EmptyArg;

Although, as a procedural macro, #[apply] can't be banned:

#[apply] // same as `#[apply()]`
struct Oops;

No runtime deps