Skip to content

Commit

Permalink
feat: add chain!
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Jan 26, 2025
1 parent 179a501 commit 450429a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
53 changes: 53 additions & 0 deletions const-str/src/__ctfe/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// Chains multiple macro calls together.
///
/// `_` is used as a placeholder for the value that is being passed through the chained calls.
///
/// # Examples
///
/// ```
/// use const_str::{chain, concat, replace, split};
///
/// const TOP: &str = "std";
///
/// const PARTS: &[&str] = &chain! {
/// stringify!(std::sync::atomic::Ordering::Relaxed),
/// replace!(_, { concat!(TOP, "::") }, ""),
/// split!(_, "::"),
/// };
///
/// assert_eq!(PARTS, &["sync", "atomic", "Ordering", "Relaxed"]);
/// ```
#[macro_export]
macro_rules! chain {
($init:expr, $( $call:ident!($($arg:tt),+), )+) => {
$crate::__chain_impl!(@chain $init, $( $call!($($arg),+) ),+)
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __chain_impl {
(@chain $init:expr, $call:ident!($($arg:tt),+)) => {
$crate::__chain_impl!(@call $init, $call!($($arg),+))
};

(@chain $init:expr, $call:ident!($($arg:tt),+), $($rest:tt)+) => {
$crate::__chain_impl!(@chain $crate::__chain_impl!(@call $init, $call!($($arg),+)), $($rest)+)
};

(@call $e: expr, $call:ident!($($arg:tt),+)) => {
$call!(
$(
$crate::__chain_impl!(@replace $e, $arg)
),+
)
};

(@replace $e:expr, _) => {
$e
};

(@replace $e:expr, $tt:tt) => {
$tt
};
}
3 changes: 3 additions & 0 deletions const-str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub mod __ctfe {
mod ascii_case;
pub use self::ascii_case::*;

mod chain;
// pub use self::chain::*;

mod compare;
pub use self::compare::*;

Expand Down

0 comments on commit 450429a

Please sign in to comment.