#cfg #remove #macro #call

macro remove_macro_call

Attribute macro that removes a call of a function-like macro

4 releases

Uses new Rust 2021

0.1.3 Feb 18, 2022
0.1.2 Feb 18, 2022
0.1.1 Feb 13, 2022
0.1.0 Feb 13, 2022

#414 in Rust patterns

Download history 33/week @ 2022-06-05 6/week @ 2022-06-12 9/week @ 2022-06-19 5/week @ 2022-06-26 8/week @ 2022-07-03 8/week @ 2022-07-10 33/week @ 2022-07-17 16/week @ 2022-07-24 32/week @ 2022-07-31 17/week @ 2022-08-07 11/week @ 2022-08-14 14/week @ 2022-08-21 8/week @ 2022-08-28 13/week @ 2022-09-04 14/week @ 2022-09-11 11/week @ 2022-09-18

52 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

9KB

Attribute macro that removes a call of a function-like macro

In a vaccum, remove_macro_call attribute is fairly useless because unconditional application of #[remove_macro_call] attribute to either declarative or procedural function-like macros yields the same result as writing only the code inside of the enclosing punctuation (parentheses, braces, or square brackets).

Example

use remove_macro_call::remove_macro_call;

macro_rules! reorder_statements {
    ($s1:stmt; $s2:stmt;) => {
        $s2;
        $s1;
    };
}

let mut n = 2;
reorder_statements! {
    n += 2;
    n *= 2;
}

// 2*2+2 = 6
assert_eq!(n, 6);

// The new variable shadows the old one
let mut n = 2;

n += 2;
n *= 2;

// (2+2)*2 = 8
assert_eq!(n, 8);

// The new variable shadows the old one
let mut n = 2;
#[remove_macro_call]
reorder_statements! {
    n += 2;
    n *= 2;
}

// (2+2)*2 = 8
assert_eq!(n, 8);

However, with cfg_attr attribute remove_macro_call allows one to remove macro calls conditionally. One important application of such combination is providing support for stable toolchain while also providing functionality relying on Nightly features.

Example

#![cfg_attr(feature = "const_trait_impl", feature(const_trait_impl))]
#![cfg_attr(feature = "const_default_impls", feature(const_default_impls))]
#![cfg_attr(feature = "const_fn_trait_bound", feature(const_fn_trait_bound))]

#[cfg(not(all(
    feature = "const_trait_impl",
    feature = "const_default_impls",
    feature = "const_fn_trait_bound"
)))]
use const_trait_impl::unconst_trait_impl;
use core::{default::Default, marker::PhantomData};
#[cfg(all(
    feature = "const_trait_impl",
    feature = "const_default_impls",
    feature = "const_fn_trait_bound"
))]
use remove_macro_call::remove_macro_call;

// Since ZST is both Eq and and PartialEq, it has structural match
// https://github.com/rust-lang/rust/issues/63438
#[derive(Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd, Copy)]
pub struct ZST<T: ?Sized>(PhantomData<T>);

pub trait TraitName {}

#[cfg_attr(
    all(
        feature = "const_trait_impl",
        feature = "const_default_impls",
        feature = "const_fn_trait_bound"
    ),
    remove_macro_call
)]
unconst_trait_impl! {
    impl<T: ?Sized> const TraitName for ZST<T> {}
}

// With `cargo build --features const_trait_impl, const_default_impls, const_fn_trait_bound`
// or with `cargo build --all-features, the code below is expanded as is. Otherwise,
// it gets "unconsted" to be supported by stable toolchain.
#[cfg_attr(
    all(
        feature = "const_trait_impl",
        feature = "const_default_impls",
        feature = "const_fn_trait_bound"
    ),
    remove_macro_call
)]
unconst_trait_impl! {
    impl<T: ~const TraitName + ?Sized> const Default for ZST<T> {
        fn default() -> Self {
            ZST(Default::default())
        }
    }
}

Note: In the real code, the example above could be replaced with a simpler version relying on cfg_aliases crate.

Real-world examples:

You can learn more about const_trait_impl here:

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~220–630KB
~15K SLoC