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 43 44 45 46 47
/// Constructs an [`AsciiStr`] constant from an ascii string,
///
/// # Compile-time errors
///
/// This macro produces a compile-time error by indexing an empty array with
/// the index of the first non-ascii byte.
///
/// # Example
///
/// ```rust
/// use const_format::ascii_str;
///
/// let fooo = ascii_str!("hello");
///
/// assert_eq!(fooo.as_str(), "hello");
///
/// // You can pass constants as arguments!
/// const BAR_S: &str = "world";
/// let bar = ascii_str!(BAR_S);
///
/// assert_eq!(bar.as_str(), "world");
///
/// ```
///
/// ```compile_fail
/// use const_format::ascii_str;
///
/// let fooo = ascii_str!("Γειά σου Κόσμε!");
///
/// ```
///
/// [`AsciiStr`]: ./struct.AsciiStr.html
///
#[cfg_attr(feature = "__docsrs", doc(cfg(feature = "fmt")))]
#[cfg(feature = "fmt")]
#[macro_export]
macro_rules! ascii_str {
($str:expr $(,)*) => {{
const __CF_ASCII_STR_CONSTANT: $crate::AsciiStr<'static> = {
match $crate::AsciiStr::new($str.as_bytes()) {
Ok(x) => x,
$crate::pmr::Err(e) => [][e.invalid_from],
}
};
__CF_ASCII_STR_CONSTANT
}};
}