about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorJethro Beekman <jethro@fortanix.com>2019-02-14 18:06:01 +0530
committerJethro Beekman <jethro@fortanix.com>2019-02-28 19:09:17 -0800
commitc0e8cf94103289c424c62ca48e1e3f56e352a84a (patch)
treee85558f334a16ee69e3495099e13cdf38e6dc666 /src/libstd/io
parent350674b7180a41c8e508d93c6ab8e203b69d3df7 (diff)
downloadrust-c0e8cf94103289c424c62ca48e1e3f56e352a84a.tar.gz
rust-c0e8cf94103289c424c62ca48e1e3f56e352a84a.zip
Use the correct stderr when testing libstd
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/impls.rs14
-rw-r--r--src/libstd/io/stdio.rs16
2 files changed, 28 insertions, 2 deletions
diff --git a/src/libstd/io/impls.rs b/src/libstd/io/impls.rs
index bd3d0a41638..b286e4016da 100644
--- a/src/libstd/io/impls.rs
+++ b/src/libstd/io/impls.rs
@@ -165,6 +165,20 @@ impl<B: BufRead + ?Sized> BufRead for Box<B> {
     }
 }
 
+// Used by panicking::default_hook
+#[cfg(test)]
+/// This impl is only used by printing logic, so any error returned is always
+/// of kind `Other`, and should be ignored.
+impl Write for Box<dyn (::realstd::io::Write) + Send> {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        (**self).write(buf).map_err(|_| ErrorKind::Other.into())
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        (**self).flush().map_err(|_| ErrorKind::Other.into())
+    }
+}
+
 // =============================================================================
 // In-memory buffer implementations
 
diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs
index 13bf357e2eb..589fb455a19 100644
--- a/src/libstd/io/stdio.rs
+++ b/src/libstd/io/stdio.rs
@@ -1,3 +1,5 @@
+#![cfg_attr(test, allow(unused))]
+
 use crate::io::prelude::*;
 
 use crate::cell::RefCell;
@@ -16,6 +18,13 @@ thread_local! {
     }
 }
 
+/// Stderr used by eprint! and eprintln! macros, and panics
+thread_local! {
+    static LOCAL_STDERR: RefCell<Option<Box<dyn 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
@@ -668,7 +677,6 @@ impl fmt::Debug for StderrLock<'_> {
            issue = "0")]
 #[doc(hidden)]
 pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
-    use crate::panicking::LOCAL_STDERR;
     use crate::mem;
     LOCAL_STDERR.with(move |slot| {
         mem::replace(&mut *slot.borrow_mut(), sink)
@@ -740,6 +748,7 @@ where
            reason = "implementation detail which may disappear or be replaced at any time",
            issue = "0")]
 #[doc(hidden)]
+#[cfg(not(test))]
 pub fn _print(args: fmt::Arguments) {
     print_to(args, &LOCAL_STDOUT, stdout, "stdout");
 }
@@ -748,12 +757,15 @@ pub fn _print(args: fmt::Arguments) {
            reason = "implementation detail which may disappear or be replaced at any time",
            issue = "0")]
 #[doc(hidden)]
+#[cfg(not(test))]
 pub fn _eprint(args: fmt::Arguments) {
-    use crate::panicking::LOCAL_STDERR;
     print_to(args, &LOCAL_STDERR, stderr, "stderr");
 }
 
 #[cfg(test)]
+pub use realstd::io::{_eprint, _print};
+
+#[cfg(test)]
 mod tests {
     use crate::panic::{UnwindSafe, RefUnwindSafe};
     use crate::thread;