From 2d6e8b41da9a3c41d1e86eb259069a4326e2bae2 Mon Sep 17 00:00:00 2001 From: Klim Tsoutsman Date: Tue, 27 Jun 2023 03:39:44 +1000 Subject: [PATCH] Implement `println!` using nested `format_args!` (#987) Our previous usage of `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}"); ``` 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)*)); }); }