Crate apply_macro[][src]

Expand description

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;

Attribute Macros

apply

The main attribute macro of this crate.