mod inner_mod {
macro_rules! opt_format {
() => {
(String::new(), true)
};
(@capture $($args:tt)*) => {
(format!($($args)*), false)
};
(@nocapture $str:literal) => {
(String::from($str), true)
};
($msg:tt $(,)?) => {
format_args_conditional::branch_on_format_capture!(
$crate::inner_mod::opt_format {@capture},
$crate::inner_mod::opt_format {@nocapture},
$msg
)
};
($($args:tt)*) => {
$crate::inner_mod::opt_format!(@capture $($args)*)
};
}
pub(crate) use opt_format;
}
#[test]
fn optimizing_format() {
let x = 1;
macro_rules! test_opt_format {
($expect_opt:ident, $($tokens:tt)*) => {
assert_eq!(inner_mod::opt_format!($($tokens)*), (format!($($tokens)*), $expect_opt));
};
}
test_opt_format!(true, "hello");
test_opt_format!(false, "hello {x}");
test_opt_format!(false, "hello {}", 2);
test_opt_format!(false, "hello {x}", x = 3);
test_opt_format!(true, "hello {{x}}");
}