diff options
| author | Simonas Kazlauskas <git@kazlauskas.me> | 2015-03-09 00:30:15 +0200 |
|---|---|---|
| committer | Simonas Kazlauskas <git@kazlauskas.me> | 2015-03-15 00:26:17 +0200 |
| commit | 6e92f0580b0bfe8433df73a9139eaa72c47258b2 (patch) | |
| tree | 294c43242cbeccf72d1868cbbee366d4af108bf7 /src/libstd | |
| parent | 3e4be02b80a3dd27bce20870958fe0aef7e7336d (diff) | |
| download | rust-6e92f0580b0bfe8433df73a9139eaa72c47258b2.tar.gz rust-6e92f0580b0bfe8433df73a9139eaa72c47258b2.zip | |
Use new io in print and println macroses
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/mod.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/stdio.rs | 52 | ||||
| -rw-r--r-- | src/libstd/macros.rs | 15 | ||||
| -rw-r--r-- | src/libstd/old_io/stdio.rs | 14 |
4 files changed, 58 insertions, 27 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 3fddaaad807..aadc2dc83af 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -37,10 +37,10 @@ pub use self::buffered::IntoInnerError; pub use self::cursor::Cursor; pub use self::error::{Result, Error, ErrorKind}; pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; -pub use self::stdio::{stdin, stdout, stderr, Stdin, Stdout, Stderr}; +pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; #[doc(no_inline, hidden)] -pub use self::stdio::set_panic; +pub use self::stdio::{set_panic, set_print}; #[macro_use] mod lazy; diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 3b4e396953d..fed58ed4b64 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -11,6 +11,7 @@ use prelude::v1::*; use io::prelude::*; +use cell::RefCell; use cmp; use fmt; use io::lazy::Lazy; @@ -18,6 +19,13 @@ use io::{self, BufReader, LineWriter}; use sync::{Arc, Mutex, MutexGuard}; use sys::stdio; +/// Stdout used by print! and println! macroses +thread_local! { + static LOCAL_STDOUT: RefCell<Option<Box<Write + Send>>> = { + RefCell::new(None) + } +} + /// A handle to a raw instance of the standard input stream of this process. /// /// This handle is not synchronized or buffered in any fashion. Constructed via @@ -347,15 +355,15 @@ impl<'a> Write for StderrLock<'a> { fn flush(&mut self) -> io::Result<()> { self.inner.flush() } } -/// Resets the task-local stdout handle to the specified writer +/// Resets the task-local stderr handle to the specified writer /// -/// This will replace the current task's stdout handle, returning the old -/// handle. All future calls to `print` and friends will emit their output to +/// This will replace the current task's stderr handle, returning the old +/// handle. All future calls to `panic!` and friends will emit their output to /// this specified handle. /// /// Note that this does not need to be called for all new tasks; the default -/// output handle is to the process's stdout stream. -#[unstable(feature = "set_panic", +/// output handle is to the process's stderr stream. +#[unstable(feature = "set_stdio", reason = "this function may disappear completely or be replaced \ with a more general mechanism")] #[doc(hidden)] @@ -369,3 +377,37 @@ pub fn set_panic(sink: Box<Write + Send>) -> Option<Box<Write + Send>> { Some(s) }) } + +/// Resets the task-local stdout handle to the specified writer +/// +/// This will replace the current task's stdout handle, returning the old +/// handle. All future calls to `print!` and friends will emit their output to +/// this specified handle. +/// +/// Note that this does not need to be called for all new tasks; the default +/// output handle is to the process's stdout stream. +#[unstable(feature = "set_stdio", + reason = "this function may disappear completely or be replaced \ + with a more general mechanism")] +#[doc(hidden)] +pub fn set_print(sink: Box<Write + Send>) -> Option<Box<Write + Send>> { + use mem; + LOCAL_STDOUT.with(move |slot| { + mem::replace(&mut *slot.borrow_mut(), Some(sink)) + }).and_then(|mut s| { + let _ = s.flush(); + Some(s) + }) +} + +#[unstable(feature = "print", + reason = "implementation detail which may disappear or be replaced at any time")] +#[doc(hidden)] +pub fn _print(args: fmt::Arguments) { + if let Err(e) = LOCAL_STDOUT.with(|s| match s.borrow_mut().as_mut() { + Some(w) => w.write_fmt(args), + None => stdout().write_fmt(args) + }) { + panic!("failed printing to stdout: {}", e); + } +} diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index a5022943267..7378f43f757 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -60,19 +60,21 @@ macro_rules! panic { }); } +/// Macro for printing to the standard output. +/// /// Equivalent to the `println!` macro except that a newline is not printed at /// the end of the message. #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] +#[allow_internal_unstable] macro_rules! print { - ($($arg:tt)*) => ($crate::old_io::stdio::print_args(format_args!($($arg)*))) + ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); } -/// Macro for printing to a task's stdout handle. +/// Macro for printing to the standard output. /// -/// Each task can override its stdout handle via `std::old_io::stdio::set_stdout`. -/// The syntax of this macro is the same as that used for `format!`. For more -/// information, see `std::fmt` and `std::old_io::stdio`. +/// Use the `format!` syntax to write data to the standard output. +/// See `std::fmt` for more information. /// /// # Examples /// @@ -83,7 +85,8 @@ macro_rules! print { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! println { - ($($arg:tt)*) => ($crate::old_io::stdio::println_args(format_args!($($arg)*))) + ($fmt:expr) => (print!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); } /// Helper macro for unwrapping `Result` values while returning early with an diff --git a/src/libstd/old_io/stdio.rs b/src/libstd/old_io/stdio.rs index 34b4ec94a44..dcc34505730 100644 --- a/src/libstd/old_io/stdio.rs +++ b/src/libstd/old_io/stdio.rs @@ -535,18 +535,4 @@ mod tests { stdout(); stderr(); } - - #[test] - fn capture_stdout() { - use old_io::{ChanReader, ChanWriter}; - - let (tx, rx) = channel(); - let (mut r, w) = (ChanReader::new(rx), ChanWriter::new(tx)); - // FIXME (#22405): Replace `Box::new` with `box` here when/if possible. - let _t = thread::spawn(move|| { - set_stdout(Box::new(w)); - println!("hello!"); - }); - assert_eq!(r.read_to_string().unwrap(), "hello!\n"); - } } |
