Skip to content

Commit 097afd4

Browse files
committed
add io::stdio
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
1 parent 2c9ac10 commit 097afd4

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ mod repeat;
5555
mod sink;
5656
mod stderr;
5757
mod stdin;
58+
mod stdio;
5859
mod stdout;
5960
mod timeout;

src/io/stdio.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Internal types for stdio.
2+
//!
3+
//! This module is a port of `libstd/io/stdio.rs`,and contains internal types for `print`/`eprint`.
4+
5+
use crate::io::{stderr, stdout, Write};
6+
use std::fmt;
7+
8+
/// Write `args` `global_s`. `label` identifies the stream in a panic message.
9+
async fn print_to<T>(
10+
args: fmt::Arguments<'_>,
11+
global_s: fn() -> T,
12+
label: &str,
13+
) where
14+
T: Write,
15+
{
16+
if let Err(e) = global_s().write_fmt(args).await {
17+
panic!("failed printing to {}: {}", label, e);
18+
}
19+
}
20+
21+
#[doc(hidden)]
22+
pub async fn _print(args: fmt::Arguments<'_>) {
23+
print_to(args, stdout, "stdout");
24+
}
25+
26+
#[doc(hidden)]
27+
pub async fn _eprint(args: fmt::Arguments<'_>) {
28+
print_to(args, stderr, "stderr");
29+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ cfg_if! {
7676
}
7777
}
7878

79+
mod macros;
7980
pub(crate) mod utils;
8081

8182
#[doc(inline)]

src/macros.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Prints to the standard output.
2+
///
3+
/// Equivalent to the [`println!`] macro except that a newline is not printed at
4+
/// the end of the message.
5+
///
6+
/// Note that stdout is frequently line-buffered by default so it may be
7+
/// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted
8+
/// immediately.
9+
///
10+
/// Use `print!` only for the primary output of your program. Use
11+
/// [`eprint!`] instead to print error and progress messages.
12+
///
13+
/// [`println!`]: macro.println.html
14+
/// [flush]: io/trait.Write.html#tymethod.flush
15+
/// [`eprint!`]: macro.eprint.html
16+
///
17+
/// # Panics
18+
///
19+
/// Panics if writing to `io::stdout()` fails.
20+
///
21+
/// # Examples
22+
///
23+
/// ```
24+
/// use std::io::{self, Write};
25+
///
26+
/// print!("this ");
27+
/// print!("will ");
28+
/// print!("be ");
29+
/// print!("on ");
30+
/// print!("the ");
31+
/// print!("same ");
32+
/// print!("line ");
33+
///
34+
/// io::stdout().flush().unwrap();
35+
///
36+
/// print!("this string has a newline, why not choose println! instead?\n");
37+
///
38+
/// io::stdout().flush().unwrap();
39+
/// ```
40+
#[macro_export]
41+
macro_rules! print {
42+
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
43+
}

0 commit comments

Comments
 (0)