add print macros

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
stabilize-from-stream
Yoshua Wuyts 5 years ago
parent 04479b13c3
commit 296d0d9d31
No known key found for this signature in database
GPG Key ID: 24EA8164F96777ED

@ -39,6 +39,10 @@ pub use stdout::{stdout, Stdout};
pub use timeout::timeout; pub use timeout::timeout;
pub use write::Write; pub use write::Write;
// For use in the print macros.
#[doc(hidden)]
pub use stdio::{_eprint, _print};
pub mod prelude; pub mod prelude;
pub(crate) mod buf_read; pub(crate) mod buf_read;

@ -2,28 +2,20 @@
//! //!
//! This module is a port of `libstd/io/stdio.rs`,and contains internal types for `print`/`eprint`. //! This module is a port of `libstd/io/stdio.rs`,and contains internal types for `print`/`eprint`.
use crate::io::{stderr, stdout, Write}; use crate::prelude::*;
use crate::io::{stderr, stdout};
use std::fmt; use std::fmt;
/// Write `args` `global_s`. `label` identifies the stream in a panic message.
async fn print_to<T>(
args: fmt::Arguments<'_>,
global_s: fn() -> T,
label: &str,
) where
T: Write,
{
if let Err(e) = global_s().write_fmt(args).await {
panic!("failed printing to {}: {}", label, e);
}
}
#[doc(hidden)] #[doc(hidden)]
pub async fn _print(args: fmt::Arguments<'_>) { pub async fn _print(args: fmt::Arguments<'_>) {
print_to(args, stdout, "stdout"); if let Err(e) = stdout().write_fmt(args).await {
panic!("failed printing to stdout: {}", e);
}
} }
#[doc(hidden)] #[doc(hidden)]
pub async fn _eprint(args: fmt::Arguments<'_>) { pub async fn _eprint(args: fmt::Arguments<'_>) {
print_to(args, stderr, "stderr"); if let Err(e) = stderr().write_fmt(args).await {
panic!("failed printing to stderr: {}", e);
}
} }

@ -36,7 +36,7 @@ impl<T: Write + Unpin + ?Sized> Future for WriteFmtFuture<'_, T> {
// Copy the data from the buffer into the writer until it's done. // Copy the data from the buffer into the writer until it's done.
loop { loop {
if buffer.is_empty() { if *amt == buffer.len() as u64 {
futures_core::ready!(Pin::new(&mut **writer).poll_flush(cx))?; futures_core::ready!(Pin::new(&mut **writer).poll_flush(cx))?;
return Poll::Ready(Ok(())); return Poll::Ready(Ok(()));
} }

@ -21,23 +21,135 @@
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use std::io::{self, Write}; /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::io;
/// use async_std::print;
/// ///
/// print!("this "); /// print!("this ").await;
/// print!("will "); /// print!("will ").await;
/// print!("be "); /// print!("be ").await;
/// print!("on "); /// print!("on ").await;
/// print!("the "); /// print!("the ").await;
/// print!("same "); /// print!("same ").await;
/// print!("line "); /// print!("line ").await;
/// ///
/// io::stdout().flush().unwrap(); /// io::stdout().flush().await.unwrap();
/// ///
/// print!("this string has a newline, why not choose println! instead?\n"); /// print!("this string has a newline, why not choose println! instead?\n").await;
/// ///
/// io::stdout().flush().unwrap(); /// io::stdout().flush().await.unwrap();
/// #
/// # Ok(()) }) }
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! print { macro_rules! print {
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)))
}
/// Prints to the standard output, with a newline.
///
/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone
/// (no additional CARRIAGE RETURN (`\r`/`U+000D`)).
///
/// Use the [`format!`] syntax to write data to the standard output.
/// See [`std::fmt`] for more information.
///
/// Use `println!` only for the primary output of your program. Use
/// [`eprintln!`] instead to print error and progress messages.
///
/// [`format!`]: macro.format.html
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
/// [`eprintln!`]: macro.eprintln.html
/// # Panics
///
/// Panics if writing to `io::stdout` fails.
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::println;
///
/// println!().await; // prints just a newline
/// println!("hello there!").await;
/// println!("format {} arguments", "some").await;
/// #
/// # Ok(()) }) }
/// ```
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)))
}
/// Prints to the standard error.
///
/// Equivalent to the [`print!`] macro, except that output goes to
/// [`io::stderr`] instead of `io::stdout`. See [`print!`] for
/// example usage.
///
/// Use `eprint!` only for error and progress messages. Use `print!`
/// instead for the primary output of your program.
///
/// [`io::stderr`]: io/struct.Stderr.html
/// [`print!`]: macro.print.html
///
/// # Panics
///
/// Panics if writing to `io::stderr` fails.
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::eprint;
///
/// eprint!("Error: Could not complete task").await;
/// #
/// # Ok(()) }) }
/// ```
#[macro_export]
macro_rules! eprint {
($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)))
}
/// Prints to the standard error, with a newline.
///
/// Equivalent to the [`println!`] macro, except that output goes to
/// [`io::stderr`] instead of `io::stdout`. See [`println!`] for
/// example usage.
///
/// Use `eprintln!` only for error and progress messages. Use `println!`
/// instead for the primary output of your program.
///
/// [`io::stderr`]: io/struct.Stderr.html
/// [`println!`]: macro.println.html
///
/// # Panics
///
/// Panics if writing to `io::stderr` fails.
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::eprintln;
///
/// eprintln!("Error: Could not complete task").await;
/// #
/// # Ok(()) }) }
/// ```
#[macro_export]
macro_rules! eprintln {
() => (async { $crate::eprint!("\n").await; });
($($arg:tt)*) => (
async {
$crate::io::_eprint(format_args!($($arg)*)).await;
}
);
} }

Loading…
Cancel
Save