Skip to content

Commit

Permalink
Make write/print macros eagerly drop temporaries
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 22, 2022
1 parent ae29890 commit 0502496
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,10 @@ macro_rules! r#try {
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
macro_rules! write {
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args!($($arg)*))
};
($dst:expr, $($arg:tt)*) => {{
let result = $dst.write_fmt($crate::format_args!($($arg)*));
result
}};
}

/// Write formatted data into a buffer, with a newline appended.
Expand Down Expand Up @@ -553,9 +554,10 @@ macro_rules! writeln {
($dst:expr $(,)?) => {
$crate::write!($dst, "\n")
};
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args_nl!($($arg)*))
};
($dst:expr, $($arg:tt)*) => {{
let result = $dst.write_fmt($crate::format_args_nl!($($arg)*));
result
}};
}

/// Indicates unreachable code.
Expand Down
12 changes: 6 additions & 6 deletions library/std/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ macro_rules! panic {
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
#[allow_internal_unstable(print_internals)]
macro_rules! print {
($($arg:tt)*) => {
$crate::io::_print($crate::format_args!($($arg)*))
};
($($arg:tt)*) => {{
$crate::io::_print($crate::format_args!($($arg)*));
}};
}

/// Prints to the standard output, with a newline.
Expand Down Expand Up @@ -133,9 +133,9 @@ macro_rules! println {
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
#[allow_internal_unstable(print_internals)]
macro_rules! eprint {
($($arg:tt)*) => {
$crate::io::_eprint($crate::format_args!($($arg)*))
};
($($arg:tt)*) => {{
$crate::io::_eprint($crate::format_args!($($arg)*));
}};
}

/// Prints to the standard error, with a newline.
Expand Down

0 comments on commit 0502496

Please sign in to comment.