1111use prelude:: v1:: * ;
1212use io:: prelude:: * ;
1313
14+ use cell:: RefCell ;
1415use cmp;
1516use fmt;
1617use io:: lazy:: Lazy ;
1718use io:: { self , BufReader , LineWriter } ;
1819use sync:: { Arc , Mutex , MutexGuard } ;
1920use sys:: stdio;
2021
22+ /// Stdout used by print! and println! macroses
23+ thread_local ! {
24+ static LOCAL_STDOUT : RefCell <Option <Box <Write + Send >>> = {
25+ RefCell :: new( None )
26+ }
27+ }
28+
2129/// A handle to a raw instance of the standard input stream of this process.
2230///
2331/// This handle is not synchronized or buffered in any fashion. Constructed via
@@ -347,15 +355,15 @@ impl<'a> Write for StderrLock<'a> {
347355 fn flush ( & mut self ) -> io:: Result < ( ) > { self . inner . flush ( ) }
348356}
349357
350- /// Resets the task-local stdout handle to the specified writer
358+ /// Resets the task-local stderr handle to the specified writer
351359///
352- /// This will replace the current task's stdout handle, returning the old
353- /// handle. All future calls to `print ` and friends will emit their output to
360+ /// This will replace the current task's stderr handle, returning the old
361+ /// handle. All future calls to `panic! ` and friends will emit their output to
354362/// this specified handle.
355363///
356364/// Note that this does not need to be called for all new tasks; the default
357- /// output handle is to the process's stdout stream.
358- #[ unstable( feature = "set_panic " ,
365+ /// output handle is to the process's stderr stream.
366+ #[ unstable( feature = "set_stdio " ,
359367 reason = "this function may disappear completely or be replaced \
360368 with a more general mechanism") ]
361369#[ doc( hidden) ]
@@ -369,3 +377,37 @@ pub fn set_panic(sink: Box<Write + Send>) -> Option<Box<Write + Send>> {
369377 Some ( s)
370378 } )
371379}
380+
381+ /// Resets the task-local stdout handle to the specified writer
382+ ///
383+ /// This will replace the current task's stdout handle, returning the old
384+ /// handle. All future calls to `print!` and friends will emit their output to
385+ /// this specified handle.
386+ ///
387+ /// Note that this does not need to be called for all new tasks; the default
388+ /// output handle is to the process's stdout stream.
389+ #[ unstable( feature = "set_stdio" ,
390+ reason = "this function may disappear completely or be replaced \
391+ with a more general mechanism") ]
392+ #[ doc( hidden) ]
393+ pub fn set_print ( sink : Box < Write + Send > ) -> Option < Box < Write + Send > > {
394+ use mem;
395+ LOCAL_STDOUT . with ( move |slot| {
396+ mem:: replace ( & mut * slot. borrow_mut ( ) , Some ( sink) )
397+ } ) . and_then ( |mut s| {
398+ let _ = s. flush ( ) ;
399+ Some ( s)
400+ } )
401+ }
402+
403+ #[ unstable( feature = "print" ,
404+ reason = "implementation detail which may disappear or be replaced at any time" ) ]
405+ #[ doc( hidden) ]
406+ pub fn _print ( args : fmt:: Arguments ) {
407+ if let Err ( e) = LOCAL_STDOUT . with ( |s| match s. borrow_mut ( ) . as_mut ( ) {
408+ Some ( w) => w. write_fmt ( args) ,
409+ None => stdout ( ) . write_fmt ( args)
410+ } ) {
411+ panic ! ( "failed printing to stdout: {}" , e) ;
412+ }
413+ }
0 commit comments