Skip to content

Commit

Permalink
Implement println! using nested format_args! (#987)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tsoutsman authored Jun 26, 2023
1 parent bb66e6a commit 2d6e8b4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kernel/app_io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ pub fn line_discipline() -> Result<Arc<LineDiscipline>, &'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)*)));
});

}

Expand All @@ -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)*));
});
}

Expand Down

0 comments on commit 2d6e8b4

Please sign in to comment.