@@ -16,7 +16,7 @@ use crate::sys_common;
16
16
use crate :: sys_common:: remutex:: { ReentrantMutex , ReentrantMutexGuard } ;
17
17
use crate :: thread:: LocalKey ;
18
18
19
- type LocalStream = Arc < Mutex < dyn Write + Send > > ;
19
+ type LocalStream = Arc < Mutex < Vec < u8 > > > ;
20
20
21
21
thread_local ! {
22
22
/// Used by the test crate to capture the output of the print! and println! macros.
@@ -911,13 +911,8 @@ pub fn set_panic(sink: Option<LocalStream>) -> Option<LocalStream> {
911
911
// LOCAL_STDERR is definitely None since LOCAL_STREAMS is false.
912
912
return None ;
913
913
}
914
- let s =
915
- LOCAL_STDERR . with ( move |slot| mem:: replace ( & mut * slot. borrow_mut ( ) , sink) ) . and_then ( |s| {
916
- let _ = s. lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . flush ( ) ;
917
- Some ( s)
918
- } ) ;
919
914
LOCAL_STREAMS . store ( true , Ordering :: Relaxed ) ;
920
- s
915
+ LOCAL_STDERR . with ( move |slot| mem :: replace ( & mut * slot . borrow_mut ( ) , sink ) )
921
916
}
922
917
923
918
/// Resets the thread-local stdout handle to the specified writer
@@ -941,13 +936,8 @@ pub fn set_print(sink: Option<LocalStream>) -> Option<LocalStream> {
941
936
// LOCAL_STDOUT is definitely None since LOCAL_STREAMS is false.
942
937
return None ;
943
938
}
944
- let s =
945
- LOCAL_STDOUT . with ( move |slot| mem:: replace ( & mut * slot. borrow_mut ( ) , sink) ) . and_then ( |s| {
946
- let _ = s. lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . flush ( ) ;
947
- Some ( s)
948
- } ) ;
949
939
LOCAL_STREAMS . store ( true , Ordering :: Relaxed ) ;
950
- s
940
+ LOCAL_STDOUT . with ( move |slot| mem :: replace ( & mut * slot . borrow_mut ( ) , sink ) )
951
941
}
952
942
953
943
pub ( crate ) fn clone_io ( ) -> ( Option < LocalStream > , Option < LocalStream > ) {
@@ -956,9 +946,10 @@ pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
956
946
return ( None , None ) ;
957
947
}
958
948
959
- LOCAL_STDOUT . with ( |stdout| {
960
- LOCAL_STDERR . with ( |stderr| ( stdout. borrow ( ) . clone ( ) , stderr. borrow ( ) . clone ( ) ) )
961
- } )
949
+ (
950
+ LOCAL_STDOUT . with ( |s| s. borrow ( ) . clone ( ) ) ,
951
+ LOCAL_STDERR . with ( |s| s. borrow ( ) . clone ( ) ) ,
952
+ )
962
953
}
963
954
964
955
/// Write `args` to output stream `local_s` if possible, `global_s`
@@ -979,28 +970,22 @@ fn print_to<T>(
979
970
) where
980
971
T : Write ,
981
972
{
982
- let result = LOCAL_STREAMS
983
- . load ( Ordering :: Relaxed )
984
- . then ( || {
985
- local_s
986
- . try_with ( |s| {
987
- // Note that we completely remove a local sink to write to in case
988
- // our printing recursively panics/prints, so the recursive
989
- // panic/print goes to the global sink instead of our local sink.
990
- let prev = s. borrow_mut ( ) . take ( ) ;
991
- if let Some ( w) = prev {
992
- let result = w. lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . write_fmt ( args) ;
993
- * s. borrow_mut ( ) = Some ( w) ;
994
- return result;
995
- }
996
- global_s ( ) . write_fmt ( args)
997
- } )
998
- . ok ( )
999
- } )
1000
- . flatten ( )
1001
- . unwrap_or_else ( || global_s ( ) . write_fmt ( args) ) ;
1002
-
1003
- if let Err ( e) = result {
973
+ if LOCAL_STREAMS . load ( Ordering :: Relaxed )
974
+ && local_s. try_with ( |s| {
975
+ // Note that we completely remove a local sink to write to in case
976
+ // our printing recursively panics/prints, so the recursive
977
+ // panic/print goes to the global sink instead of our local sink.
978
+ s. take ( ) . map ( |w| {
979
+ let _ = w. lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . write_fmt ( args) ;
980
+ * s. borrow_mut ( ) = Some ( w) ;
981
+ } )
982
+ } ) == Ok ( Some ( ( ) ) )
983
+ {
984
+ // Succesfully wrote to local stream.
985
+ return ;
986
+ }
987
+
988
+ if let Err ( e) = global_s ( ) . write_fmt ( args) {
1004
989
panic ! ( "failed printing to {}: {}" , label, e) ;
1005
990
}
1006
991
}
0 commit comments