devela/code/util/ident.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// devela::code::util::ident
//
//! identifier related macro functionality
//
/// Defines a constant for every given identifier with a value of its index in the list.
/// # Examples
/// ```
/// # use devela::ident_const_index;
/// ident_const_index![pub, 3; first, second, third]; // with commas
/// assert![0 == first && 1 == second && 2 == third];
///
/// ident_const_index![pub(crate), 2; fourth fifth]; // without commas
/// assert![0 == fourth && 1 == fifth];
/// ```
// USEDBY: enumset macro
#[macro_export]
#[cfg_attr(cargo_primary_package, doc(hidden))]
macro_rules! ident_const_index {
( // without commas:
// $vis: the visibility of the constants (pub, pub(super), …).
// $total: the total number of identifiers.
// $ident, $($rest),*: the list of identifiers.
$vis:vis, $total:expr; $ident:ident $($rest:ident)*
) => { $crate::paste! {
#[allow(non_upper_case_globals)]
$vis const $ident: usize = $total - $crate::ident_total!($($rest)*) - 1;
$( $crate::ident_const_index!($vis, $total; $rest); )*
}};
( // with commas:
// $vis: the visibility of the constants (pub, pub(super), …).
// $total: the total number of identifiers.
// $ident, $($rest),*: the list of identifiers.
$vis:vis, $total:expr; $ident:ident $(, $($rest:ident),* $(,)? )?
) => { $crate::paste! {
#[allow(non_upper_case_globals)]
$vis const $ident: usize = $total - $crate::ident_total!( $($($rest)*)? ) - 1;
$( $crate::ident_const_index!($vis, $total; $($rest),*); )?
}};
}
#[doc(inline)]
pub use ident_const_index;