Skip to content

Commit de597fc

Browse files
committed
Optimize set_{panic,print}(None).
1 parent ed3ead0 commit de597fc

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

library/std/src/io/stdio.rs

+8
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,10 @@ impl fmt::Debug for StderrLock<'_> {
905905
#[doc(hidden)]
906906
pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
907907
use crate::mem;
908+
if sink.is_none() && !LOCAL_STREAMS.load(Ordering::Relaxed) {
909+
// LOCAL_STDERR is definitely None since LOCAL_STREAMS is false.
910+
return None;
911+
}
908912
let s = LOCAL_STDERR.with(move |slot| mem::replace(&mut *slot.borrow_mut(), sink)).and_then(
909913
|mut s| {
910914
let _ = s.flush();
@@ -932,6 +936,10 @@ pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write +
932936
#[doc(hidden)]
933937
pub fn set_print(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
934938
use crate::mem;
939+
if sink.is_none() && !LOCAL_STREAMS.load(Ordering::Relaxed) {
940+
// LOCAL_STDOUT is definitely None since LOCAL_STREAMS is false.
941+
return None;
942+
}
935943
let s = LOCAL_STDOUT.with(move |slot| mem::replace(&mut *slot.borrow_mut(), sink)).and_then(
936944
|mut s| {
937945
let _ = s.flush();

0 commit comments

Comments
 (0)