forked from mirror/async-std
add io::stdio
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
This commit is contained in:
parent
33da049717
commit
04479b13c3
4 changed files with 74 additions and 0 deletions
|
@ -55,5 +55,6 @@ mod repeat;
|
|||
mod sink;
|
||||
mod stderr;
|
||||
mod stdin;
|
||||
mod stdio;
|
||||
mod stdout;
|
||||
mod timeout;
|
||||
|
|
29
src/io/stdio.rs
Normal file
29
src/io/stdio.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
//! Internal types for stdio.
|
||||
//!
|
||||
//! This module is a port of `libstd/io/stdio.rs`,and contains internal types for `print`/`eprint`.
|
||||
|
||||
use crate::io::{stderr, stdout, Write};
|
||||
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)]
|
||||
pub async fn _print(args: fmt::Arguments<'_>) {
|
||||
print_to(args, stdout, "stdout");
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub async fn _eprint(args: fmt::Arguments<'_>) {
|
||||
print_to(args, stderr, "stderr");
|
||||
}
|
|
@ -76,6 +76,7 @@ cfg_if! {
|
|||
}
|
||||
}
|
||||
|
||||
mod macros;
|
||||
pub(crate) mod utils;
|
||||
|
||||
#[doc(inline)]
|
||||
|
|
43
src/macros.rs
Normal file
43
src/macros.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
/// Prints to the standard output.
|
||||
///
|
||||
/// Equivalent to the [`println!`] macro except that a newline is not printed at
|
||||
/// the end of the message.
|
||||
///
|
||||
/// Note that stdout is frequently line-buffered by default so it may be
|
||||
/// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted
|
||||
/// immediately.
|
||||
///
|
||||
/// Use `print!` only for the primary output of your program. Use
|
||||
/// [`eprint!`] instead to print error and progress messages.
|
||||
///
|
||||
/// [`println!`]: macro.println.html
|
||||
/// [flush]: io/trait.Write.html#tymethod.flush
|
||||
/// [`eprint!`]: macro.eprint.html
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if writing to `io::stdout()` fails.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::io::{self, Write};
|
||||
///
|
||||
/// print!("this ");
|
||||
/// print!("will ");
|
||||
/// print!("be ");
|
||||
/// print!("on ");
|
||||
/// print!("the ");
|
||||
/// print!("same ");
|
||||
/// print!("line ");
|
||||
///
|
||||
/// io::stdout().flush().unwrap();
|
||||
///
|
||||
/// print!("this string has a newline, why not choose println! instead?\n");
|
||||
///
|
||||
/// io::stdout().flush().unwrap();
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
|
||||
}
|
Loading…
Reference in a new issue