From a97216aa93da5100736a3cbbb68d2bdf074cf898 Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Wed, 7 Jan 2026 13:16:07 +1100 Subject: [PATCH] misc(macros.rs): `{e}print{ln}` use `format_args!()` Allows to capture identifiers in format strings. Signed-off-by: Anhad Singh --- src/macros.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index c04ca3bc26..2c988281ec 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,35 +1,41 @@ /// Print to stdout #[macro_export] macro_rules! print { - ($($arg:tt)*) => ({ + ($($arg:tt)*) => {{ use core::fmt::Write; - let _ = write!($crate::platform::FileWriter::new(1), $($arg)*); - }); + let _ = $crate::platform::FileWriter::new(1).write_fmt(format_args!($($arg)*)); + }}; } /// Print with new line to stdout #[macro_export] macro_rules! println { - () => (print!("\n")); - ($fmt:expr) => (print!(concat!($fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); + () => { + $crate::print!("\n") + }; + ($($arg:tt)*) => { + $crate::print!("{}\n", format_args!($($arg)*)) + }; } /// Print to stderr #[macro_export] macro_rules! eprint { - ($($arg:tt)*) => ({ + ($($arg:tt)*) => {{ use core::fmt::Write; - let _ = write!($crate::platform::FileWriter::new(2), $($arg)*); - }); + let _ = $crate::platform::FileWriter::new(2).write_fmt(format_args!($($arg)*)); + }}; } /// Print with new line to stderr #[macro_export] macro_rules! eprintln { - () => (eprint!("\n")); - ($fmt:expr) => (eprint!(concat!($fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (eprint!(concat!($fmt, "\n"), $($arg)*)); + () => { + $crate::eprint!("\n") + }; + ($($arg:tt)*) => { + $crate::eprint!("{}\n", format_args!($($arg)*)) + }; } /// Lifted from libstd