File tree Expand file tree Collapse file tree 4 files changed +74
-0
lines changed Expand file tree Collapse file tree 4 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -55,5 +55,6 @@ mod repeat;
5555mod sink;
5656mod stderr;
5757mod stdin;
58+ mod stdio;
5859mod stdout;
5960mod timeout;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ cfg_if! {
7676 }
7777}
7878
79+ mod macros;
7980pub ( crate ) mod utils;
8081
8182#[ doc( inline) ]
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments