11
11
use prelude:: v1:: * ;
12
12
use io:: prelude:: * ;
13
13
14
- use cell:: RefCell ;
15
14
use cmp;
16
15
use fmt;
17
16
use io:: lazy:: Lazy ;
18
17
use io:: { self , BufReader , LineWriter } ;
19
18
use sync:: { Arc , Mutex , MutexGuard } ;
20
19
use sys:: stdio;
21
20
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
-
29
21
/// A handle to a raw instance of the standard input stream of this process.
30
22
///
31
23
/// This handle is not synchronized or buffered in any fashion. Constructed via
32
- /// the `std::io::stdio:: stdin_raw` function.
33
- struct StdinRaw ( stdio:: Stdin ) ;
24
+ /// the `std::io::stdin_raw` function.
25
+ pub struct StdinRaw ( stdio:: Stdin ) ;
34
26
35
27
/// A handle to a raw instance of the standard output stream of this process.
36
28
///
37
29
/// This handle is not synchronized or buffered in any fashion. Constructed via
38
- /// the `std::io::stdio:: stdout_raw` function.
39
- struct StdoutRaw ( stdio:: Stdout ) ;
30
+ /// the `std::io::stdout_raw` function.
31
+ pub struct StdoutRaw ( stdio:: Stdout ) ;
40
32
41
33
/// A handle to a raw instance of the standard output stream of this process.
42
34
///
43
35
/// This handle is not synchronized or buffered in any fashion. Constructed via
44
- /// the `std::io::stdio:: stderr_raw` function.
45
- struct StderrRaw ( stdio:: Stderr ) ;
36
+ /// the `std::io::stderr_raw` function.
37
+ pub struct StderrRaw ( stdio:: Stderr ) ;
46
38
47
39
/// Construct a new raw handle to the standard input of this process.
48
40
///
@@ -51,7 +43,7 @@ struct StderrRaw(stdio::Stderr);
51
43
/// handles is **not** available to raw handles returned from this function.
52
44
///
53
45
/// The returned handle has no external synchronization or buffering.
54
- fn stdin_raw ( ) -> StdinRaw { StdinRaw ( stdio:: Stdin :: new ( ) ) }
46
+ pub fn stdin_raw ( ) -> StdinRaw { StdinRaw ( stdio:: Stdin :: new ( ) ) }
55
47
56
48
/// Construct a new raw handle to the standard input stream of this process.
57
49
///
@@ -62,7 +54,7 @@ fn stdin_raw() -> StdinRaw { StdinRaw(stdio::Stdin::new()) }
62
54
///
63
55
/// The returned handle has no external synchronization or buffering layered on
64
56
/// top.
65
- fn stdout_raw ( ) -> StdoutRaw { StdoutRaw ( stdio:: Stdout :: new ( ) ) }
57
+ pub fn stdout_raw ( ) -> StdoutRaw { StdoutRaw ( stdio:: Stdout :: new ( ) ) }
66
58
67
59
/// Construct a new raw handle to the standard input stream of this process.
68
60
///
@@ -71,7 +63,7 @@ fn stdout_raw() -> StdoutRaw { StdoutRaw(stdio::Stdout::new()) }
71
63
///
72
64
/// The returned handle has no external synchronization or buffering layered on
73
65
/// top.
74
- fn stderr_raw ( ) -> StderrRaw { StderrRaw ( stdio:: Stderr :: new ( ) ) }
66
+ pub fn stderr_raw ( ) -> StderrRaw { StderrRaw ( stdio:: Stderr :: new ( ) ) }
75
67
76
68
impl Read for StdinRaw {
77
69
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > { self . 0 . read ( buf) }
@@ -117,6 +109,9 @@ pub struct StdinLock<'a> {
117
109
/// The `Read` trait is implemented for the returned value but the `BufRead`
118
110
/// trait is not due to the global nature of the standard input stream. The
119
111
/// locked version, `StdinLock`, implements both `Read` and `BufRead`, however.
112
+ ///
113
+ /// To avoid locking and buffering altogether, it is recommended to use the
114
+ /// `stdin_raw` constructor.
120
115
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
121
116
pub fn stdin ( ) -> Stdin {
122
117
static INSTANCE : Lazy < Mutex < BufReader < StdinRaw > > > = lazy_init ! ( stdin_init) ;
@@ -229,6 +224,9 @@ pub struct StdoutLock<'a> {
229
224
/// provided via the `lock` method.
230
225
///
231
226
/// The returned handle implements the `Write` trait.
227
+ ///
228
+ /// To avoid locking and buffering altogether, it is recommended to use the
229
+ /// `stdout_raw` constructor.
232
230
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
233
231
pub fn stdout ( ) -> Stdout {
234
232
static INSTANCE : Lazy < Mutex < LineWriter < StdoutRaw > > > = lazy_init ! ( stdout_init) ;
@@ -299,6 +297,9 @@ pub struct StderrLock<'a> {
299
297
/// this function. No handles are buffered, however.
300
298
///
301
299
/// The returned handle implements the `Write` trait.
300
+ ///
301
+ /// To avoid locking altogether, it is recommended to use the `stderr_raw`
302
+ /// constructor.
302
303
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
303
304
pub fn stderr ( ) -> Stderr {
304
305
static INSTANCE : Lazy < Mutex < StderrRaw > > = lazy_init ! ( stderr_init) ;
@@ -346,59 +347,25 @@ impl<'a> Write for StderrLock<'a> {
346
347
fn flush ( & mut self ) -> io:: Result < ( ) > { self . inner . flush ( ) }
347
348
}
348
349
349
- /// Resets the task-local stderr handle to the specified writer
350
- ///
351
- /// This will replace the current task's stderr handle, returning the old
352
- /// handle. All future calls to `panic!` and friends will emit their output to
353
- /// this specified handle.
354
- ///
355
- /// Note that this does not need to be called for all new tasks; the default
356
- /// output handle is to the process's stderr stream.
357
- #[ unstable( feature = "set_stdio" ,
358
- reason = "this function may disappear completely or be replaced \
359
- with a more general mechanism") ]
360
- #[ doc( hidden) ]
361
- pub fn set_panic ( sink : Box < Write + Send > ) -> Option < Box < Write + Send > > {
362
- use panicking:: LOCAL_STDERR ;
363
- use mem;
364
- LOCAL_STDERR . with ( move |slot| {
365
- mem:: replace ( & mut * slot. borrow_mut ( ) , Some ( sink) )
366
- } ) . and_then ( |mut s| {
367
- let _ = s. flush ( ) ;
368
- Some ( s)
369
- } )
370
- }
371
-
372
350
/// Resets the task-local stdout handle to the specified writer
373
351
///
374
352
/// This will replace the current task's stdout handle, returning the old
375
- /// handle. All future calls to `print! ` and friends will emit their output to
353
+ /// handle. All future calls to `print` and friends will emit their output to
376
354
/// this specified handle.
377
355
///
378
356
/// Note that this does not need to be called for all new tasks; the default
379
357
/// output handle is to the process's stdout stream.
380
- #[ unstable( feature = "set_stdio " ,
358
+ #[ unstable( feature = "set_panic " ,
381
359
reason = "this function may disappear completely or be replaced \
382
360
with a more general mechanism") ]
383
361
#[ doc( hidden) ]
384
- pub fn set_print ( sink : Box < Write + Send > ) -> Option < Box < Write + Send > > {
362
+ pub fn set_panic ( sink : Box < Write + Send > ) -> Option < Box < Write + Send > > {
363
+ use panicking:: LOCAL_STDERR ;
385
364
use mem;
386
- LOCAL_STDOUT . with ( move |slot| {
365
+ LOCAL_STDERR . with ( move |slot| {
387
366
mem:: replace ( & mut * slot. borrow_mut ( ) , Some ( sink) )
388
367
} ) . and_then ( |mut s| {
389
368
let _ = s. flush ( ) ;
390
369
Some ( s)
391
370
} )
392
371
}
393
-
394
- #[ unstable( feature = "print" ,
395
- reason = "implementation detail which may disappear or be replaced at any time" ) ]
396
- #[ doc( hidden) ]
397
- pub fn _print ( args : fmt:: Arguments ) {
398
- if let Err ( e) = LOCAL_STDOUT . with ( |s| match s. borrow_mut ( ) . as_mut ( ) {
399
- Some ( w) => w. write_fmt ( args) ,
400
- None => stdout ( ) . write_fmt ( args)
401
- } ) {
402
- panic ! ( "failed printing to stdout: {}" , e) ;
403
- }
404
- }
0 commit comments