@@ -50,8 +50,9 @@ struct StderrRaw(stdio::Stderr);
50
50
/// handles is **not** available to raw handles returned from this function.
51
51
///
52
52
/// The returned handle has no external synchronization or buffering.
53
- fn stdin_raw ( ) -> io:: Result < StdinRaw > {
54
- stdio:: Stdin :: new ( ) . map ( StdinRaw )
53
+ #[ unstable( feature = "libstd_sys_internals" , issue = "none" ) ]
54
+ const fn stdin_raw ( ) -> StdinRaw {
55
+ StdinRaw ( stdio:: Stdin :: new ( ) )
55
56
}
56
57
57
58
/// Constructs a new raw handle to the standard output stream of this process.
@@ -63,8 +64,9 @@ fn stdin_raw() -> io::Result<StdinRaw> {
63
64
///
64
65
/// The returned handle has no external synchronization or buffering layered on
65
66
/// top.
66
- fn stdout_raw ( ) -> io:: Result < StdoutRaw > {
67
- stdio:: Stdout :: new ( ) . map ( StdoutRaw )
67
+ #[ unstable( feature = "libstd_sys_internals" , issue = "none" ) ]
68
+ const fn stdout_raw ( ) -> StdoutRaw {
69
+ StdoutRaw ( stdio:: Stdout :: new ( ) )
68
70
}
69
71
70
72
/// Constructs a new raw handle to the standard error stream of this process.
@@ -74,17 +76,18 @@ fn stdout_raw() -> io::Result<StdoutRaw> {
74
76
///
75
77
/// The returned handle has no external synchronization or buffering layered on
76
78
/// top.
77
- fn stderr_raw ( ) -> io:: Result < StderrRaw > {
78
- stdio:: Stderr :: new ( ) . map ( StderrRaw )
79
+ #[ unstable( feature = "libstd_sys_internals" , issue = "none" ) ]
80
+ const fn stderr_raw ( ) -> StderrRaw {
81
+ StderrRaw ( stdio:: Stderr :: new ( ) )
79
82
}
80
83
81
84
impl Read for StdinRaw {
82
85
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
83
- self . 0 . read ( buf)
86
+ handle_ebadf ( self . 0 . read ( buf) , 0 )
84
87
}
85
88
86
89
fn read_vectored ( & mut self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
87
- self . 0 . read_vectored ( bufs)
90
+ handle_ebadf ( self . 0 . read_vectored ( bufs) , 0 )
88
91
}
89
92
90
93
#[ inline]
@@ -98,25 +101,22 @@ impl Read for StdinRaw {
98
101
}
99
102
100
103
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
101
- self . 0 . read_to_end ( buf)
104
+ handle_ebadf ( self . 0 . read_to_end ( buf) , 0 )
102
105
}
103
106
104
107
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
105
- self . 0 . read_to_string ( buf)
106
- }
107
-
108
- fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
109
- self . 0 . read_exact ( buf)
108
+ handle_ebadf ( self . 0 . read_to_string ( buf) , 0 )
110
109
}
111
110
}
112
111
113
112
impl Write for StdoutRaw {
114
113
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
115
- self . 0 . write ( buf)
114
+ handle_ebadf ( self . 0 . write ( buf) , buf . len ( ) )
116
115
}
117
116
118
117
fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
119
- self . 0 . write_vectored ( bufs)
118
+ let total = bufs. iter ( ) . map ( |b| b. len ( ) ) . sum ( ) ;
119
+ handle_ebadf ( self . 0 . write_vectored ( bufs) , total)
120
120
}
121
121
122
122
#[ inline]
@@ -125,29 +125,30 @@ impl Write for StdoutRaw {
125
125
}
126
126
127
127
fn flush ( & mut self ) -> io:: Result < ( ) > {
128
- self . 0 . flush ( )
128
+ handle_ebadf ( self . 0 . flush ( ) , ( ) )
129
129
}
130
130
131
131
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
132
- self . 0 . write_all ( buf)
132
+ handle_ebadf ( self . 0 . write_all ( buf) , ( ) )
133
133
}
134
134
135
135
fn write_all_vectored ( & mut self , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
136
- self . 0 . write_all_vectored ( bufs)
136
+ handle_ebadf ( self . 0 . write_all_vectored ( bufs) , ( ) )
137
137
}
138
138
139
139
fn write_fmt ( & mut self , fmt : fmt:: Arguments < ' _ > ) -> io:: Result < ( ) > {
140
- self . 0 . write_fmt ( fmt)
140
+ handle_ebadf ( self . 0 . write_fmt ( fmt) , ( ) )
141
141
}
142
142
}
143
143
144
144
impl Write for StderrRaw {
145
145
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
146
- self . 0 . write ( buf)
146
+ handle_ebadf ( self . 0 . write ( buf) , buf . len ( ) )
147
147
}
148
148
149
149
fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
150
- self . 0 . write_vectored ( bufs)
150
+ let total = bufs. iter ( ) . map ( |b| b. len ( ) ) . sum ( ) ;
151
+ handle_ebadf ( self . 0 . write_vectored ( bufs) , total)
151
152
}
152
153
153
154
#[ inline]
@@ -156,80 +157,19 @@ impl Write for StderrRaw {
156
157
}
157
158
158
159
fn flush ( & mut self ) -> io:: Result < ( ) > {
159
- self . 0 . flush ( )
160
+ handle_ebadf ( self . 0 . flush ( ) , ( ) )
160
161
}
161
162
162
163
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
163
- self . 0 . write_all ( buf)
164
+ handle_ebadf ( self . 0 . write_all ( buf) , ( ) )
164
165
}
165
166
166
167
fn write_all_vectored ( & mut self , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
167
- self . 0 . write_all_vectored ( bufs)
168
+ handle_ebadf ( self . 0 . write_all_vectored ( bufs) , ( ) )
168
169
}
169
170
170
171
fn write_fmt ( & mut self , fmt : fmt:: Arguments < ' _ > ) -> io:: Result < ( ) > {
171
- self . 0 . write_fmt ( fmt)
172
- }
173
- }
174
-
175
- enum Maybe < T > {
176
- Real ( T ) ,
177
- Fake ,
178
- }
179
-
180
- impl < W : io:: Write > io:: Write for Maybe < W > {
181
- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
182
- match * self {
183
- Maybe :: Real ( ref mut w) => handle_ebadf ( w. write ( buf) , buf. len ( ) ) ,
184
- Maybe :: Fake => Ok ( buf. len ( ) ) ,
185
- }
186
- }
187
-
188
- fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
189
- let total = bufs. iter ( ) . map ( |b| b. len ( ) ) . sum ( ) ;
190
- match self {
191
- Maybe :: Real ( w) => handle_ebadf ( w. write_vectored ( bufs) , total) ,
192
- Maybe :: Fake => Ok ( total) ,
193
- }
194
- }
195
-
196
- #[ inline]
197
- fn is_write_vectored ( & self ) -> bool {
198
- match self {
199
- Maybe :: Real ( w) => w. is_write_vectored ( ) ,
200
- Maybe :: Fake => true ,
201
- }
202
- }
203
-
204
- fn flush ( & mut self ) -> io:: Result < ( ) > {
205
- match * self {
206
- Maybe :: Real ( ref mut w) => handle_ebadf ( w. flush ( ) , ( ) ) ,
207
- Maybe :: Fake => Ok ( ( ) ) ,
208
- }
209
- }
210
- }
211
-
212
- impl < R : io:: Read > io:: Read for Maybe < R > {
213
- fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
214
- match * self {
215
- Maybe :: Real ( ref mut r) => handle_ebadf ( r. read ( buf) , 0 ) ,
216
- Maybe :: Fake => Ok ( 0 ) ,
217
- }
218
- }
219
-
220
- fn read_vectored ( & mut self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
221
- match self {
222
- Maybe :: Real ( r) => handle_ebadf ( r. read_vectored ( bufs) , 0 ) ,
223
- Maybe :: Fake => Ok ( 0 ) ,
224
- }
225
- }
226
-
227
- #[ inline]
228
- fn is_read_vectored ( & self ) -> bool {
229
- match self {
230
- Maybe :: Real ( w) => w. is_read_vectored ( ) ,
231
- Maybe :: Fake => true ,
232
- }
172
+ handle_ebadf ( self . 0 . write_fmt ( fmt) , ( ) )
233
173
}
234
174
}
235
175
@@ -274,7 +214,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
274
214
/// ```
275
215
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
276
216
pub struct Stdin {
277
- inner : Arc < Mutex < BufReader < Maybe < StdinRaw > > > > ,
217
+ inner : Arc < Mutex < BufReader < StdinRaw > > > ,
278
218
}
279
219
280
220
/// A locked reference to the `Stdin` handle.
@@ -305,7 +245,7 @@ pub struct Stdin {
305
245
/// ```
306
246
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
307
247
pub struct StdinLock < ' a > {
308
- inner : MutexGuard < ' a , BufReader < Maybe < StdinRaw > > > ,
248
+ inner : MutexGuard < ' a , BufReader < StdinRaw > > ,
309
249
}
310
250
311
251
/// Constructs a new handle to the standard input of the current process.
@@ -349,18 +289,14 @@ pub struct StdinLock<'a> {
349
289
/// ```
350
290
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
351
291
pub fn stdin ( ) -> Stdin {
352
- static INSTANCE : Lazy < Mutex < BufReader < Maybe < StdinRaw > > > > = Lazy :: new ( ) ;
292
+ static INSTANCE : Lazy < Mutex < BufReader < StdinRaw > > > = Lazy :: new ( ) ;
353
293
return Stdin {
354
294
inner : unsafe { INSTANCE . get ( stdin_init) . expect ( "cannot access stdin during shutdown" ) } ,
355
295
} ;
356
296
357
- fn stdin_init ( ) -> Arc < Mutex < BufReader < Maybe < StdinRaw > > > > {
297
+ fn stdin_init ( ) -> Arc < Mutex < BufReader < StdinRaw > > > {
358
298
// This must not reentrantly access `INSTANCE`
359
- let stdin = match stdin_raw ( ) {
360
- Ok ( stdin) => Maybe :: Real ( stdin) ,
361
- _ => Maybe :: Fake ,
362
- } ;
363
-
299
+ let stdin = stdin_raw ( ) ;
364
300
Arc :: new ( Mutex :: new ( BufReader :: with_capacity ( stdio:: STDIN_BUF_SIZE , stdin) ) )
365
301
}
366
302
}
@@ -537,7 +473,7 @@ pub struct Stdout {
537
473
// FIXME: this should be LineWriter or BufWriter depending on the state of
538
474
// stdout (tty or not). Note that if this is not line buffered it
539
475
// should also flush-on-panic or some form of flush-on-abort.
540
- inner : Arc < ReentrantMutex < RefCell < LineWriter < Maybe < StdoutRaw > > > > > ,
476
+ inner : Arc < ReentrantMutex < RefCell < LineWriter < StdoutRaw > > > > ,
541
477
}
542
478
543
479
/// A locked reference to the `Stdout` handle.
@@ -551,7 +487,7 @@ pub struct Stdout {
551
487
/// an error.
552
488
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
553
489
pub struct StdoutLock < ' a > {
554
- inner : ReentrantMutexGuard < ' a , RefCell < LineWriter < Maybe < StdoutRaw > > > > ,
490
+ inner : ReentrantMutexGuard < ' a , RefCell < LineWriter < StdoutRaw > > > ,
555
491
}
556
492
557
493
/// Constructs a new handle to the standard output of the current process.
@@ -595,17 +531,14 @@ pub struct StdoutLock<'a> {
595
531
/// ```
596
532
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
597
533
pub fn stdout ( ) -> Stdout {
598
- static INSTANCE : Lazy < ReentrantMutex < RefCell < LineWriter < Maybe < StdoutRaw > > > > > = Lazy :: new ( ) ;
534
+ static INSTANCE : Lazy < ReentrantMutex < RefCell < LineWriter < StdoutRaw > > > > = Lazy :: new ( ) ;
599
535
return Stdout {
600
536
inner : unsafe { INSTANCE . get ( stdout_init) . expect ( "cannot access stdout during shutdown" ) } ,
601
537
} ;
602
538
603
- fn stdout_init ( ) -> Arc < ReentrantMutex < RefCell < LineWriter < Maybe < StdoutRaw > > > > > {
539
+ fn stdout_init ( ) -> Arc < ReentrantMutex < RefCell < LineWriter < StdoutRaw > > > > {
604
540
// This must not reentrantly access `INSTANCE`
605
- let stdout = match stdout_raw ( ) {
606
- Ok ( stdout) => Maybe :: Real ( stdout) ,
607
- _ => Maybe :: Fake ,
608
- } ;
541
+ let stdout = stdout_raw ( ) ;
609
542
unsafe {
610
543
let ret = Arc :: new ( ReentrantMutex :: new ( RefCell :: new ( LineWriter :: new ( stdout) ) ) ) ;
611
544
ret. init ( ) ;
@@ -715,7 +648,7 @@ impl fmt::Debug for StdoutLock<'_> {
715
648
/// an error.
716
649
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
717
650
pub struct Stderr {
718
- inner : & ' static ReentrantMutex < RefCell < Maybe < StderrRaw > > > ,
651
+ inner : & ' static ReentrantMutex < RefCell < StderrRaw > > ,
719
652
}
720
653
721
654
/// A locked reference to the `Stderr` handle.
@@ -729,7 +662,7 @@ pub struct Stderr {
729
662
/// an error.
730
663
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
731
664
pub struct StderrLock < ' a > {
732
- inner : ReentrantMutexGuard < ' a , RefCell < Maybe < StderrRaw > > > ,
665
+ inner : ReentrantMutexGuard < ' a , RefCell < StderrRaw > > ,
733
666
}
734
667
735
668
/// Constructs a new handle to the standard error of the current process.
@@ -778,19 +711,14 @@ pub fn stderr() -> Stderr {
778
711
//
779
712
// This has the added benefit of allowing `stderr` to be usable during
780
713
// process shutdown as well!
781
- static INSTANCE : ReentrantMutex < RefCell < Maybe < StderrRaw > > > =
782
- unsafe { ReentrantMutex :: new ( RefCell :: new ( Maybe :: Fake ) ) } ;
714
+ static INSTANCE : ReentrantMutex < RefCell < StderrRaw > > =
715
+ unsafe { ReentrantMutex :: new ( RefCell :: new ( stderr_raw ( ) ) ) } ;
783
716
784
717
// When accessing stderr we need one-time initialization of the reentrant
785
- // mutex, followed by one-time detection of whether we actually have a
786
- // stderr handle or not. Afterwards we can just always use the now-filled-in
787
- // `INSTANCE` value.
718
+ // mutex. Afterwards we can just always use the now-filled-in `INSTANCE` value.
788
719
static INIT : Once = Once :: new ( ) ;
789
720
INIT . call_once ( || unsafe {
790
721
INSTANCE . init ( ) ;
791
- if let Ok ( stderr) = stderr_raw ( ) {
792
- * INSTANCE . lock ( ) . borrow_mut ( ) = Maybe :: Real ( stderr) ;
793
- }
794
722
} ) ;
795
723
Stderr { inner : & INSTANCE }
796
724
}
0 commit comments