]> git.lizzy.rs Git - rust.git/commitdiff
Refactor stderr_prints_nothing into a more modular function
authorJethro Beekman <jethro@fortanix.com>
Mon, 27 Aug 2018 16:57:51 +0000 (09:57 -0700)
committerJethro Beekman <jethro@fortanix.com>
Thu, 6 Dec 2018 15:07:15 +0000 (20:37 +0530)
src/libstd/panicking.rs
src/libstd/sys/cloudabi/stdio.rs
src/libstd/sys/redox/stdio.rs
src/libstd/sys/unix/stdio.rs
src/libstd/sys/wasm/stdio.rs
src/libstd/sys/windows/stdio.rs
src/libstd/sys_common/util.rs

index 4930d3566081494061fa617082b8badf6b0cbe4c..b70d56f9e593d7fc259eb129873e309e10db3770 100644 (file)
@@ -29,7 +29,7 @@
 use mem;
 use ptr;
 use raw;
-use sys::stdio::{Stderr, stderr_prints_nothing};
+use sys::stdio::panic_output;
 use sys_common::rwlock::RWLock;
 use sys_common::thread_info;
 use sys_common::util;
@@ -193,7 +193,6 @@ fn default_hook(info: &PanicInfo) {
             None => "Box<Any>",
         }
     };
-    let mut err = Stderr::new().ok();
     let thread = thread_info::current_thread();
     let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
 
@@ -215,17 +214,14 @@ fn default_hook(info: &PanicInfo) {
         }
     };
 
-    let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
-    match (prev, err.as_mut()) {
-       (Some(mut stderr), _) => {
-           write(&mut *stderr);
-           let mut s = Some(stderr);
-           LOCAL_STDERR.with(|slot| {
-               *slot.borrow_mut() = s.take();
-           });
-       }
-       (None, Some(ref mut err)) => { write(err) }
-       _ => {}
+    if let Some(mut local) = LOCAL_STDERR.with(|s| s.borrow_mut().take()) {
+       write(&mut *local);
+       let mut s = Some(local);
+       LOCAL_STDERR.with(|slot| {
+           *slot.borrow_mut() = s.take();
+       });
+    } else if let Some(mut out) = panic_output() {
+        write(&mut out);
     }
 }
 
@@ -485,7 +481,7 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
             // Some platforms know that printing to stderr won't ever actually
             // print anything, and if that's the case we can skip the default
             // hook.
-            Hook::Default if stderr_prints_nothing() => {}
+            Hook::Default if panic_output().is_none() => {}
             Hook::Default => {
                 info.set_payload(payload.get());
                 default_hook(&info);
@@ -494,7 +490,7 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
                 info.set_payload(payload.get());
                 (*ptr)(&info);
             }
-        }
+        };
         HOOK_LOCK.read_unlock();
     }
 
index 1d7344f921c9d564d30bac0c09f86bcf7b682995..c90dbd8beab869ea1cab1ebea50dc511c3a440a6 100644 (file)
@@ -78,6 +78,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
 
 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
 
-pub fn stderr_prints_nothing() -> bool {
-    false
+pub fn panic_output() -> Option<impl io::Write> {
+    Stderr::new().ok()
 }
index 7a4d11b0ecb9a8c64f2845898755d6883608544e..52cd9334ffbbf4ab7b9c0c987ad57327446397d0 100644 (file)
@@ -76,6 +76,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
 
 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
 
-pub fn stderr_prints_nothing() -> bool {
-    false
+pub fn panic_output() -> Option<impl io::Write> {
+    Stderr::new().ok()
 }
index 87ba2aef4f1d3dfe1e499f089b55adc4e6df5bba..63e341abb2c140e56672d0409e702bc8fd6209c4 100644 (file)
@@ -76,6 +76,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
 
 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
 
-pub fn stderr_prints_nothing() -> bool {
-    false
+pub fn panic_output() -> Option<impl io::Write> {
+    Stderr::new().ok()
 }
index 023f29576a27d9c4dd667b1a035d9966de5efcfe..e51aba75333b651bcca5fdb213d631b100c5a88a 100644 (file)
@@ -70,6 +70,10 @@ pub fn is_ebadf(_err: &io::Error) -> bool {
     true
 }
 
-pub fn stderr_prints_nothing() -> bool {
-    !cfg!(feature = "wasm_syscall")
+pub fn panic_output() -> Option<impl io::Write> {
+    if cfg!(feature = "wasm_syscall") {
+        Stderr::new().ok()
+    } else {
+        None
+    }
 }
index c3a94698a0f3648093f614149b96b6a25482a0bc..61e0db87ebe118ef0ea3d50bc0e63acaa8412b8d 100644 (file)
@@ -228,6 +228,6 @@ pub fn is_ebadf(err: &io::Error) -> bool {
 // been seen to be acceptable.
 pub const STDIN_BUF_SIZE: usize = 8 * 1024;
 
-pub fn stderr_prints_nothing() -> bool {
-    false
+pub fn panic_output() -> Option<impl io::Write> {
+    Stderr::new().ok()
 }
index a373e980b970d21263aa8389ddece4cb26442365..fc86a59d17f2e93659bbe6f92fbc9548861b8de7 100644 (file)
 
 use fmt;
 use io::prelude::*;
-use sys::stdio::{Stderr, stderr_prints_nothing};
+use sys::stdio::panic_output;
 use thread;
 
 pub fn dumb_print(args: fmt::Arguments) {
-    if stderr_prints_nothing() {
-        return
+    if let Some(mut out) = panic_output() {
+        let _ = out.write_fmt(args);
     }
-    let _ = Stderr::new().map(|mut stderr| stderr.write_fmt(args));
 }
 
 // Other platforms should use the appropriate platform-specific mechanism for