| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
#[macro_export] |
| 6 |
macro_rules! tinystr { |
| 7 |
($n:literal, $s:literal) => {{ |
| 8 |
// Force it into a const context; otherwise it may get evaluated at runtime instead. |
| 9 |
const TINYSTR_MACRO_CONST: $crate::TinyAsciiStr<$n> = { |
| 10 |
match $crate::TinyAsciiStr::try_from_utf8($s.as_bytes()) { |
| 11 |
Ok(s) => s, |
| 12 |
// Cannot format the error since formatting isn't const yet |
| 13 |
Err(_) => panic!(concat!("Failed to construct tinystr from ", $s)), |
| 14 |
} |
| 15 |
}; |
| 16 |
TINYSTR_MACRO_CONST |
| 17 |
}}; |
| 18 |
} |
| 19 |
|
| 20 |
#[cfg(test)] |
| 21 |
mod tests { |
| 22 |
#[test] |
| 23 |
fn test_macro_construction() { |
| 24 |
let s1 = tinystr!(8, "foobar"); |
| 25 |
assert_eq!(&*s1, "foobar"); |
| 26 |
|
| 27 |
let s1 = tinystr!(12, "foobarbaz"); |
| 28 |
assert_eq!(&*s1, "foobarbaz"); |
| 29 |
} |
| 30 |
} |