-
Notifications
You must be signed in to change notification settings - Fork 13.4k
core and std: Optimize write*!() and print*!() for a single argument #22335
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,12 +79,25 @@ macro_rules! format { | |
|
||
/// Equivalent to the `println!` macro except that a newline is not printed at | ||
/// the end of the message. | ||
#[cfg(stage0)] | ||
#[macro_export] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
macro_rules! print { | ||
($($arg:tt)*) => ($crate::old_io::stdio::print_args(format_args!($($arg)*))) | ||
} | ||
|
||
#[cfg(not(stage0))] | ||
#[macro_export] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
macro_rules! print { | ||
($fmt:expr) => { | ||
$crate::old_io::stdio::print(format_arg!($fmt)) | ||
}; | ||
($fmt:expr, $($arg:tt)+) => { | ||
$crate::old_io::stdio::print_args(format_args!($fmt, $($arg)*)) | ||
}; | ||
} | ||
|
||
/// Macro for printing to a task's stdout handle. | ||
/// | ||
/// Each task can override its stdout handle via `std::old_io::stdio::set_stdout`. | ||
|
@@ -97,12 +110,25 @@ macro_rules! print { | |
/// println!("hello there!"); | ||
/// println!("format {} arguments", "some"); | ||
/// ``` | ||
#[cfg(stage0)] | ||
#[macro_export] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
macro_rules! println { | ||
($($arg:tt)*) => ($crate::old_io::stdio::println_args(format_args!($($arg)*))) | ||
} | ||
|
||
#[cfg(not(stage0))] | ||
#[macro_export] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
macro_rules! println { | ||
($fmt:expr) => { | ||
$crate::old_io::stdio::println(format_arg!($fmt)) | ||
}; | ||
($fmt:expr, $($arg:tt)*) => { | ||
$crate::old_io::stdio::println_args(format_args!($fmt, $($arg)*)) | ||
}; | ||
} | ||
|
||
/// Helper macro for unwrapping `Result` values while returning early with an | ||
/// error if the value of the expression is `Err`. For more information, see | ||
/// `std::io`. | ||
|
@@ -185,6 +211,28 @@ macro_rules! log { | |
/// into libsyntax itself. | ||
#[cfg(dox)] | ||
pub mod builtin { | ||
/// Parse a `&'static str` with a compatible format to `format_args!()`. | ||
/// | ||
/// This macro produces a value of type `&'static str`, and is intended to | ||
/// be used by the other formatting macros (`format!`, `write!`, `println!`) | ||
/// are proxied through this one. | ||
/// | ||
/// For more information, see the documentation in `std::fmt`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::fmt; | ||
/// | ||
/// let s = format_arg!("hello"); | ||
/// assert_eq!(s, format!("hello")); | ||
/// | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! format_arg { ($fmt:expr) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect the two cases need to be in the same macro definition to be rendered, macro_rules! format_args {
($fmt: expr) => ({ /* compiler built-in */ });
($fmt: expr, $($args: tt)*) => ({ /* compiler built-in */ });
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erickt pointed out my mistake on IRC (interpreted this just a variation of In any case, I think the doc string for this needs updating, since AFAICT, it doesn't return |
||
/* compiler built-in */ | ||
}) } | ||
|
||
/// The core macro for formatted string creation & output. | ||
/// | ||
/// This macro produces a value of type `fmt::Arguments`. This value can be | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this different from
format_args!($fmt, $($arg)*).to_string()
?