const_str/__ctfe/
chain.rs

1/// Chains multiple macro calls together.
2///
3/// `_` is used as a placeholder for the value that is being passed through the chained calls.
4///
5/// # Examples
6///
7/// ```
8/// use const_str::{chain, concat, replace, split};
9///
10/// const TOP: &str = "std";
11///
12/// const PARTS: &[&str] = &chain! {
13///    stringify!(std::sync::atomic::Ordering::Relaxed),
14///    replace!(_, { concat!(TOP, "::") }, ""),
15///    split!(_, "::"),
16/// };
17///
18/// assert_eq!(PARTS, &["sync", "atomic", "Ordering", "Relaxed"]);
19/// ```
20#[macro_export]
21macro_rules! chain {
22    ($init:expr, $( $call:ident!($($arg:tt),+), )+) => {
23        $crate::__chain_impl!(@chain $init, $( $call!($($arg),+) ),+)
24    };
25}
26
27#[doc(hidden)]
28#[macro_export]
29macro_rules! __chain_impl {
30    (@chain $init:expr, $call:ident!($($arg:tt),+)) => {
31        $crate::__chain_impl!(@call $init, $call!($($arg),+))
32    };
33
34    (@chain $init:expr, $call:ident!($($arg:tt),+), $($rest:tt)+) => {
35        $crate::__chain_impl!(@chain $crate::__chain_impl!(@call $init, $call!($($arg),+)), $($rest)+)
36    };
37
38    (@call $e: expr, $call:ident!($($arg:tt),+)) => {
39        $call!(
40            $(
41                $crate::__chain_impl!(@replace $e, $arg)
42            ),+
43        )
44    };
45
46    (@replace $e:expr, _) => {
47        $e
48    };
49
50    (@replace $e:expr, $tt:tt) => {
51        $tt
52    };
53}