apply-macro 1.0.7

An attribute macro to apply function-like macros.
Documentation

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 example:

use apply_macro::apply;

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

#[apply(common_derive)]
struct Num(i32);

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

#[apply(common_derive,)]
struct TrailingCommaIsAllowed;

assert_eq!(TrailingCommaIsAllowed, TrailingCommaIsAllowed);

The #[apply(common_derive)] on Num expands to:

# macro_rules! common_derive {
#     ($dummy:item) => {};
# }
common_derive! {
struct Num(i32);
}

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

use apply_macro::apply;

#[apply()]
#[derive()] // consistent
# #[allow(unused_attributes, dead_code)]
struct EmptyArg;

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

# use apply_macro::apply;
#[apply] // same as `#[apply()]`
# #[allow(dead_code)]
struct Oops;