@@ -50,8 +50,9 @@ struct StderrRaw(stdio::Stderr);
5050/// handles is **not** available to raw handles returned from this function.
5151///
5252/// 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 ( ) )
5556}
5657
5758/// Constructs a new raw handle to the standard output stream of this process.
@@ -63,8 +64,9 @@ fn stdin_raw() -> io::Result<StdinRaw> {
6364///
6465/// The returned handle has no external synchronization or buffering layered on
6566/// 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 ( ) )
6870}
6971
7072/// Constructs a new raw handle to the standard error stream of this process.
@@ -74,17 +76,18 @@ fn stdout_raw() -> io::Result<StdoutRaw> {
7476///
7577/// The returned handle has no external synchronization or buffering layered on
7678/// 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 ( ) )
7982}
8083
8184impl Read for StdinRaw {
8285 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
83- self . 0 . read ( buf)
86+ handle_ebadf ( self . 0 . read ( buf) , 0 )
8487 }
8588
8689 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 )
8891 }
8992
9093 #[ inline]
@@ -98,25 +101,22 @@ impl Read for StdinRaw {
98101 }
99102
100103 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 )
102105 }
103106
104107 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 )
110109 }
111110}
112111
113112impl Write for StdoutRaw {
114113 fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
115- self . 0 . write ( buf)
114+ handle_ebadf ( self . 0 . write ( buf) , buf . len ( ) )
116115 }
117116
118117 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)
120120 }
121121
122122 #[ inline]
@@ -125,29 +125,30 @@ impl Write for StdoutRaw {
125125 }
126126
127127 fn flush ( & mut self ) -> io:: Result < ( ) > {
128- self . 0 . flush ( )
128+ handle_ebadf ( self . 0 . flush ( ) , ( ) )
129129 }
130130
131131 fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
132- self . 0 . write_all ( buf)
132+ handle_ebadf ( self . 0 . write_all ( buf) , ( ) )
133133 }
134134
135135 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) , ( ) )
137137 }
138138
139139 fn write_fmt ( & mut self , fmt : fmt:: Arguments < ' _ > ) -> io:: Result < ( ) > {
140- self . 0 . write_fmt ( fmt)
140+ handle_ebadf ( self . 0 . write_fmt ( fmt) , ( ) )
141141 }
142142}
143143
144144impl Write for StderrRaw {
145145 fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
146- self . 0 . write ( buf)
146+ handle_ebadf ( self . 0 . write ( buf) , buf . len ( ) )
147147 }
148148
149149 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)
151152 }
152153
153154 #[ inline]
@@ -156,80 +157,19 @@ impl Write for StderrRaw {
156157 }
157158
158159 fn flush ( & mut self ) -> io:: Result < ( ) > {
159- self . 0 . flush ( )
160+ handle_ebadf ( self . 0 . flush ( ) , ( ) )
160161 }
161162
162163 fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
163- self . 0 . write_all ( buf)
164+ handle_ebadf ( self . 0 . write_all ( buf) , ( ) )
164165 }
165166
166167 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) , ( ) )
168169 }
169170
170171 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) , ( ) )
233173 }
234174}
235175
@@ -274,7 +214,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
274214/// ```
275215#[ stable( feature = "rust1" , since = "1.0.0" ) ]
276216pub struct Stdin {
277- inner : Arc < Mutex < BufReader < Maybe < StdinRaw > > > > ,
217+ inner : Arc < Mutex < BufReader < StdinRaw > > > ,
278218}
279219
280220/// A locked reference to the `Stdin` handle.
@@ -305,7 +245,7 @@ pub struct Stdin {
305245/// ```
306246#[ stable( feature = "rust1" , since = "1.0.0" ) ]
307247pub struct StdinLock < ' a > {
308- inner : MutexGuard < ' a , BufReader < Maybe < StdinRaw > > > ,
248+ inner : MutexGuard < ' a , BufReader < StdinRaw > > ,
309249}
310250
311251/// Constructs a new handle to the standard input of the current process.
@@ -349,18 +289,14 @@ pub struct StdinLock<'a> {
349289/// ```
350290#[ stable( feature = "rust1" , since = "1.0.0" ) ]
351291pub fn stdin ( ) -> Stdin {
352- static INSTANCE : Lazy < Mutex < BufReader < Maybe < StdinRaw > > > > = Lazy :: new ( ) ;
292+ static INSTANCE : Lazy < Mutex < BufReader < StdinRaw > > > = Lazy :: new ( ) ;
353293 return Stdin {
354294 inner : unsafe { INSTANCE . get ( stdin_init) . expect ( "cannot access stdin during shutdown" ) } ,
355295 } ;
356296
357- fn stdin_init ( ) -> Arc < Mutex < BufReader < Maybe < StdinRaw > > > > {
297+ fn stdin_init ( ) -> Arc < Mutex < BufReader < StdinRaw > > > {
358298 // 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 ( ) ;
364300 Arc :: new ( Mutex :: new ( BufReader :: with_capacity ( stdio:: STDIN_BUF_SIZE , stdin) ) )
365301 }
366302}
@@ -537,7 +473,7 @@ pub struct Stdout {
537473 // FIXME: this should be LineWriter or BufWriter depending on the state of
538474 // stdout (tty or not). Note that if this is not line buffered it
539475 // 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 > > > > ,
541477}
542478
543479/// A locked reference to the `Stdout` handle.
@@ -551,7 +487,7 @@ pub struct Stdout {
551487/// an error.
552488#[ stable( feature = "rust1" , since = "1.0.0" ) ]
553489pub struct StdoutLock < ' a > {
554- inner : ReentrantMutexGuard < ' a , RefCell < LineWriter < Maybe < StdoutRaw > > > > ,
490+ inner : ReentrantMutexGuard < ' a , RefCell < LineWriter < StdoutRaw > > > ,
555491}
556492
557493/// Constructs a new handle to the standard output of the current process.
@@ -595,17 +531,14 @@ pub struct StdoutLock<'a> {
595531/// ```
596532#[ stable( feature = "rust1" , since = "1.0.0" ) ]
597533pub fn stdout ( ) -> Stdout {
598- static INSTANCE : Lazy < ReentrantMutex < RefCell < LineWriter < Maybe < StdoutRaw > > > > > = Lazy :: new ( ) ;
534+ static INSTANCE : Lazy < ReentrantMutex < RefCell < LineWriter < StdoutRaw > > > > = Lazy :: new ( ) ;
599535 return Stdout {
600536 inner : unsafe { INSTANCE . get ( stdout_init) . expect ( "cannot access stdout during shutdown" ) } ,
601537 } ;
602538
603- fn stdout_init ( ) -> Arc < ReentrantMutex < RefCell < LineWriter < Maybe < StdoutRaw > > > > > {
539+ fn stdout_init ( ) -> Arc < ReentrantMutex < RefCell < LineWriter < StdoutRaw > > > > {
604540 // 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 ( ) ;
609542 unsafe {
610543 let ret = Arc :: new ( ReentrantMutex :: new ( RefCell :: new ( LineWriter :: new ( stdout) ) ) ) ;
611544 ret. init ( ) ;
@@ -715,7 +648,7 @@ impl fmt::Debug for StdoutLock<'_> {
715648/// an error.
716649#[ stable( feature = "rust1" , since = "1.0.0" ) ]
717650pub struct Stderr {
718- inner : & ' static ReentrantMutex < RefCell < Maybe < StderrRaw > > > ,
651+ inner : & ' static ReentrantMutex < RefCell < StderrRaw > > ,
719652}
720653
721654/// A locked reference to the `Stderr` handle.
@@ -729,7 +662,7 @@ pub struct Stderr {
729662/// an error.
730663#[ stable( feature = "rust1" , since = "1.0.0" ) ]
731664pub struct StderrLock < ' a > {
732- inner : ReentrantMutexGuard < ' a , RefCell < Maybe < StderrRaw > > > ,
665+ inner : ReentrantMutexGuard < ' a , RefCell < StderrRaw > > ,
733666}
734667
735668/// Constructs a new handle to the standard error of the current process.
@@ -778,19 +711,14 @@ pub fn stderr() -> Stderr {
778711 //
779712 // This has the added benefit of allowing `stderr` to be usable during
780713 // 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 ( ) ) ) } ;
783716
784717 // 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.
788719 static INIT : Once = Once :: new ( ) ;
789720 INIT . call_once ( || unsafe {
790721 INSTANCE . init ( ) ;
791- if let Ok ( stderr) = stderr_raw ( ) {
792- * INSTANCE . lock ( ) . borrow_mut ( ) = Maybe :: Real ( stderr) ;
793- }
794722 } ) ;
795723 Stderr { inner : & INSTANCE }
796724}
0 commit comments