| 1 | /// Constructs an [`AsciiStr`] constant from an ascii string, |
| 2 | /// |
| 3 | /// # Compile-time errors |
| 4 | /// |
| 5 | /// This macro produces a compile-time error by indexing an empty array with |
| 6 | /// the index of the first non-ascii byte. |
| 7 | /// |
| 8 | /// # Example |
| 9 | /// |
| 10 | /// ```rust |
| 11 | /// use const_format::ascii_str; |
| 12 | /// |
| 13 | /// let fooo = ascii_str!("hello"); |
| 14 | /// |
| 15 | /// assert_eq!(fooo.as_str(), "hello"); |
| 16 | /// |
| 17 | /// // You can pass constants as arguments! |
| 18 | /// const BAR_S: &str = "world"; |
| 19 | /// let bar = ascii_str!(BAR_S); |
| 20 | /// |
| 21 | /// assert_eq!(bar.as_str(), "world"); |
| 22 | /// |
| 23 | /// ``` |
| 24 | /// |
| 25 | /// ```compile_fail |
| 26 | /// use const_format::ascii_str; |
| 27 | /// |
| 28 | /// let fooo = ascii_str!("Γειά σου Κόσμε!"); |
| 29 | /// |
| 30 | /// ``` |
| 31 | /// |
| 32 | /// [`AsciiStr`]: ./struct.AsciiStr.html |
| 33 | /// |
| 34 | #[cfg_attr (feature = "__docsrs" , doc(cfg(feature = "fmt" )))] |
| 35 | #[cfg (feature = "fmt" )] |
| 36 | #[macro_export ] |
| 37 | macro_rules! ascii_str { |
| 38 | ($str:expr $(,)*) => {{ |
| 39 | const __CF_ASCII_STR_CONSTANT: $crate::AsciiStr<'static> = { |
| 40 | match $crate::AsciiStr::new($str.as_bytes()) { |
| 41 | Ok(x) => x, |
| 42 | $crate::pmr::Err(e) => [][e.invalid_from], |
| 43 | } |
| 44 | }; |
| 45 | __CF_ASCII_STR_CONSTANT |
| 46 | }}; |
| 47 | } |
| 48 | |