• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 // We are okay with panicking here because this is in a const context
13                 #[allow(clippy::panic)]
14                 // Cannot format the error since formatting isn't const yet
15                 Err(_) => panic!(concat!("Failed to construct tinystr from ", $s)),
16             }
17         };
18         TINYSTR_MACRO_CONST
19     }};
20 }
21 
22 #[cfg(test)]
23 mod tests {
24     #[test]
test_macro_construction()25     fn test_macro_construction() {
26         let s1 = tinystr!(8, "foobar");
27         assert_eq!(&*s1, "foobar");
28 
29         let s1 = tinystr!(12, "foobarbaz");
30         assert_eq!(&*s1, "foobarbaz");
31     }
32 }
33