Debian Codegraph @

src/macros.rs

raw
1 /// Creates a `CompactString` using interpolation of runtime expressions.
2 ///
3 /// The first argument `format_compact!` receives is a format string.
4 /// This must be a string literal.
5 /// The power of the formatting string is in the `{}`s contained.
6 ///
7 /// Additional parameters passed to `format_compact!` replace the `{}`s within
8 /// the formatting string in the order given unless named or
9 /// positional parameters are used; see [`std::fmt`] for more information.
10 ///
11 /// A common use for `format_compact!` is concatenation and interpolation
12 /// of strings.
13 /// The same convention is used with [`print!`] and [`write!`] macros,
14 /// depending on the intended destination of the string.
15 ///
16 /// To convert a single value to a string, use the
17 /// `ToCompactString::to_compact_string` method, which uses
18 /// the [`std::fmt::Display`] formatting trait.
19 ///
20 /// # Panics
21 ///
22 /// `format_compact!` panics if a formatting trait implementation returns
23 /// an error.
24 ///
25 /// This indicates an incorrect implementation since
26 /// `ToCompactString::to_compact_string` never returns an error itself.
27 #[macro_export]
28 macro_rules! format_compact {
29 ($($arg:tt)*) => {
30 $crate::ToCompactString::to_compact_string(&$crate::core::format_args!($($arg)*))
31 }
32 }
33
34 #[cfg(test)]
35 mod tests {
36 #[test]
37 fn test_macros() {
38 assert_eq!(format_compact!("2"), "2");
39 assert_eq!(format_compact!("{}", 2), "2");
40
41 assert!(!format_compact!("2").is_heap_allocated());
42 assert!(!format_compact!("{}", 2).is_heap_allocated());
43 }
44 }