@@ -5,7 +5,7 @@ mod tests;
5
5
6
6
use crate :: io:: prelude:: * ;
7
7
8
- use crate :: cell:: RefCell ;
8
+ use crate :: cell:: { Cell , RefCell } ;
9
9
use crate :: fmt;
10
10
use crate :: io:: { self , BufReader , Initializer , IoSlice , IoSliceMut , LineWriter } ;
11
11
use crate :: lazy:: SyncOnceCell ;
@@ -20,15 +20,15 @@ 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.
23
- static LOCAL_STDOUT : RefCell <Option <LocalStream >> = {
24
- RefCell :: new( None )
23
+ static LOCAL_STDOUT : Cell <Option <LocalStream >> = {
24
+ Cell :: new( None )
25
25
}
26
26
}
27
27
28
28
thread_local ! {
29
29
/// Used by the test crate to capture the output of the eprint! and eprintln! macros, and panics.
30
- static LOCAL_STDERR : RefCell <Option <LocalStream >> = {
31
- RefCell :: new( None )
30
+ static LOCAL_STDERR : Cell <Option <LocalStream >> = {
31
+ Cell :: new( None )
32
32
}
33
33
}
34
34
@@ -906,13 +906,12 @@ impl fmt::Debug for StderrLock<'_> {
906
906
) ]
907
907
#[ doc( hidden) ]
908
908
pub fn set_panic ( sink : Option < LocalStream > ) -> Option < LocalStream > {
909
- use crate :: mem;
910
909
if sink. is_none ( ) && !LOCAL_STREAMS . load ( Ordering :: Relaxed ) {
911
910
// LOCAL_STDERR is definitely None since LOCAL_STREAMS is false.
912
911
return None ;
913
912
}
914
913
LOCAL_STREAMS . store ( true , Ordering :: Relaxed ) ;
915
- LOCAL_STDERR . with ( move |slot| mem :: replace ( & mut * slot. borrow_mut ( ) , sink) )
914
+ LOCAL_STDERR . with ( move |slot| slot. replace ( sink) )
916
915
}
917
916
918
917
/// Resets the thread-local stdout handle to the specified writer
@@ -931,13 +930,12 @@ pub fn set_panic(sink: Option<LocalStream>) -> Option<LocalStream> {
931
930
) ]
932
931
#[ doc( hidden) ]
933
932
pub fn set_print ( sink : Option < LocalStream > ) -> Option < LocalStream > {
934
- use crate :: mem;
935
933
if sink. is_none ( ) && !LOCAL_STREAMS . load ( Ordering :: Relaxed ) {
936
934
// LOCAL_STDOUT is definitely None since LOCAL_STREAMS is false.
937
935
return None ;
938
936
}
939
937
LOCAL_STREAMS . store ( true , Ordering :: Relaxed ) ;
940
- LOCAL_STDOUT . with ( move |slot| mem :: replace ( & mut * slot. borrow_mut ( ) , sink) )
938
+ LOCAL_STDOUT . with ( move |slot| slot. replace ( sink) )
941
939
}
942
940
943
941
pub ( crate ) fn clone_io ( ) -> ( Option < LocalStream > , Option < LocalStream > ) {
@@ -946,10 +944,13 @@ pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
946
944
return ( None , None ) ;
947
945
}
948
946
949
- (
950
- LOCAL_STDOUT . with ( |s| s. borrow ( ) . clone ( ) ) ,
951
- LOCAL_STDERR . with ( |s| s. borrow ( ) . clone ( ) ) ,
952
- )
947
+ let clone = |cell : & Cell < Option < LocalStream > > | {
948
+ let s = cell. take ( ) ;
949
+ cell. set ( s. clone ( ) ) ;
950
+ s
951
+ } ;
952
+
953
+ ( LOCAL_STDOUT . with ( clone) , LOCAL_STDERR . with ( clone) )
953
954
}
954
955
955
956
/// Write `args` to output stream `local_s` if possible, `global_s`
@@ -964,7 +965,7 @@ pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
964
965
/// However, if the actual I/O causes an error, this function does panic.
965
966
fn print_to < T > (
966
967
args : fmt:: Arguments < ' _ > ,
967
- local_s : & ' static LocalKey < RefCell < Option < LocalStream > > > ,
968
+ local_s : & ' static LocalKey < Cell < Option < LocalStream > > > ,
968
969
global_s : fn ( ) -> T ,
969
970
label : & str ,
970
971
) where
@@ -977,7 +978,7 @@ fn print_to<T>(
977
978
// panic/print goes to the global sink instead of our local sink.
978
979
s. take ( ) . map ( |w| {
979
980
let _ = w. lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . write_fmt ( args) ;
980
- * s . borrow_mut ( ) = Some ( w) ;
981
+ s . set ( Some ( w) ) ;
981
982
} )
982
983
} ) == Ok ( Some ( ( ) ) )
983
984
{
0 commit comments