From ba6fdbc8fd8b1b416691f9716001c308afa3b651 Mon Sep 17 00:00:00 2001 From: Klimenty Tsoutsman Date: Sat, 24 Jun 2023 10:27:21 +1000 Subject: [PATCH] Implement `println!` using nested `format_args!` `concat!` prevents `format_args!` from capturing variables from the surrounding scope. Implementing `println!` using a nested `format_args!` removes this limitation allowing us to do the following: ```rust let x = 3; println!("{x}"); ``` Similar to `std::println!`. Also, the change [does not impact performance][1]. [1]: https://github.com/rust-lang/rust/pull/106824 Signed-off-by: Klimenty Tsoutsman --- kernel/app_io/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kernel/app_io/src/lib.rs b/kernel/app_io/src/lib.rs index f8697ba051..294b56f8a8 100644 --- a/kernel/app_io/src/lib.rs +++ b/kernel/app_io/src/lib.rs @@ -188,8 +188,10 @@ pub fn line_discipline() -> Result, &'static str> { /// Calls `print!()` with an extra newline ('\n') appended to the end. #[macro_export] macro_rules! println { - ($fmt:expr) => ($crate::print!(concat!($fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => ($crate::print!(concat!($fmt, "\n"), $($arg)*)); + () => ($crate::print!("\n")); + ($($arg:tt)*) => ({ + $crate::print_to_stdout_args(::core::format_args!("{}\n", ::core::format_args!($($arg)*))); + }); } @@ -198,7 +200,7 @@ macro_rules! println { #[macro_export] macro_rules! print { ($($arg:tt)*) => ({ - $crate::print_to_stdout_args(format_args!($($arg)*)); + $crate::print_to_stdout_args(::core::format_args!($($arg)*)); }); }