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