]> git.lizzy.rs Git - rust.git/blob - library/std/src/io/stdio.rs
2e71ad295841b00df002088559d976ed4d18faa8
[rust.git] / library / std / src / io / stdio.rs
1 #![cfg_attr(test, allow(unused))]
2
3 #[cfg(test)]
4 mod tests;
5
6 use crate::io::prelude::*;
7
8 use crate::cell::{Cell, RefCell};
9 use crate::fmt;
10 use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
11 use crate::lazy::SyncOnceCell;
12 use crate::sync::atomic::{AtomicBool, Ordering};
13 use crate::sync::{Arc, Mutex, MutexGuard};
14 use crate::sys::stdio;
15 use crate::sys_common;
16 use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
17 use crate::thread::LocalKey;
18
19 type LocalStream = Arc<Mutex<Vec<u8>>>;
20
21 thread_local! {
22     /// Used by the test crate to capture the output of the print! and println! macros.
23     static LOCAL_STDOUT: Cell<Option<LocalStream>> = {
24         Cell::new(None)
25     }
26 }
27
28 thread_local! {
29     /// Used by the test crate to capture the output of the eprint! and eprintln! macros, and panics.
30     static LOCAL_STDERR: Cell<Option<LocalStream>> = {
31         Cell::new(None)
32     }
33 }
34
35 /// Flag to indicate LOCAL_STDOUT and/or LOCAL_STDERR is used.
36 ///
37 /// If both are None and were never set on any thread, this flag is set to
38 /// false, and both LOCAL_STDOUT and LOCAL_STDOUT can be safely ignored on all
39 /// threads, saving some time and memory registering an unused thread local.
40 ///
41 /// Note about memory ordering: This contains information about whether two
42 /// thread local variables might be in use. Although this is a global flag, the
43 /// memory ordering between threads does not matter: we only want this flag to
44 /// have a consistent order between set_print/set_panic and print_to *within
45 /// the same thread*. Within the same thread, things always have a perfectly
46 /// consistent order. So Ordering::Relaxed is fine.
47 static LOCAL_STREAMS: AtomicBool = AtomicBool::new(false);
48
49 /// A handle to a raw instance of the standard input stream of this process.
50 ///
51 /// This handle is not synchronized or buffered in any fashion. Constructed via
52 /// the `std::io::stdio::stdin_raw` function.
53 struct StdinRaw(stdio::Stdin);
54
55 /// A handle to a raw instance of the standard output stream of this process.
56 ///
57 /// This handle is not synchronized or buffered in any fashion. Constructed via
58 /// the `std::io::stdio::stdout_raw` function.
59 struct StdoutRaw(stdio::Stdout);
60
61 /// A handle to a raw instance of the standard output stream of this process.
62 ///
63 /// This handle is not synchronized or buffered in any fashion. Constructed via
64 /// the `std::io::stdio::stderr_raw` function.
65 struct StderrRaw(stdio::Stderr);
66
67 /// Constructs a new raw handle to the standard input of this process.
68 ///
69 /// The returned handle does not interact with any other handles created nor
70 /// handles returned by `std::io::stdin`. Data buffered by the `std::io::stdin`
71 /// handles is **not** available to raw handles returned from this function.
72 ///
73 /// The returned handle has no external synchronization or buffering.
74 #[unstable(feature = "libstd_sys_internals", issue = "none")]
75 const fn stdin_raw() -> StdinRaw {
76     StdinRaw(stdio::Stdin::new())
77 }
78
79 /// Constructs a new raw handle to the standard output stream of this process.
80 ///
81 /// The returned handle does not interact with any other handles created nor
82 /// handles returned by `std::io::stdout`. Note that data is buffered by the
83 /// `std::io::stdout` handles so writes which happen via this raw handle may
84 /// appear before previous writes.
85 ///
86 /// The returned handle has no external synchronization or buffering layered on
87 /// top.
88 #[unstable(feature = "libstd_sys_internals", issue = "none")]
89 const fn stdout_raw() -> StdoutRaw {
90     StdoutRaw(stdio::Stdout::new())
91 }
92
93 /// Constructs a new raw handle to the standard error stream of this process.
94 ///
95 /// The returned handle does not interact with any other handles created nor
96 /// handles returned by `std::io::stderr`.
97 ///
98 /// The returned handle has no external synchronization or buffering layered on
99 /// top.
100 #[unstable(feature = "libstd_sys_internals", issue = "none")]
101 const fn stderr_raw() -> StderrRaw {
102     StderrRaw(stdio::Stderr::new())
103 }
104
105 impl Read for StdinRaw {
106     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
107         handle_ebadf(self.0.read(buf), 0)
108     }
109
110     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
111         handle_ebadf(self.0.read_vectored(bufs), 0)
112     }
113
114     #[inline]
115     fn is_read_vectored(&self) -> bool {
116         self.0.is_read_vectored()
117     }
118
119     #[inline]
120     unsafe fn initializer(&self) -> Initializer {
121         Initializer::nop()
122     }
123
124     fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
125         handle_ebadf(self.0.read_to_end(buf), 0)
126     }
127
128     fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
129         handle_ebadf(self.0.read_to_string(buf), 0)
130     }
131 }
132
133 impl Write for StdoutRaw {
134     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
135         handle_ebadf(self.0.write(buf), buf.len())
136     }
137
138     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
139         let total = bufs.iter().map(|b| b.len()).sum();
140         handle_ebadf(self.0.write_vectored(bufs), total)
141     }
142
143     #[inline]
144     fn is_write_vectored(&self) -> bool {
145         self.0.is_write_vectored()
146     }
147
148     fn flush(&mut self) -> io::Result<()> {
149         handle_ebadf(self.0.flush(), ())
150     }
151
152     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
153         handle_ebadf(self.0.write_all(buf), ())
154     }
155
156     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
157         handle_ebadf(self.0.write_all_vectored(bufs), ())
158     }
159
160     fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
161         handle_ebadf(self.0.write_fmt(fmt), ())
162     }
163 }
164
165 impl Write for StderrRaw {
166     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
167         handle_ebadf(self.0.write(buf), buf.len())
168     }
169
170     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
171         let total = bufs.iter().map(|b| b.len()).sum();
172         handle_ebadf(self.0.write_vectored(bufs), total)
173     }
174
175     #[inline]
176     fn is_write_vectored(&self) -> bool {
177         self.0.is_write_vectored()
178     }
179
180     fn flush(&mut self) -> io::Result<()> {
181         handle_ebadf(self.0.flush(), ())
182     }
183
184     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
185         handle_ebadf(self.0.write_all(buf), ())
186     }
187
188     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
189         handle_ebadf(self.0.write_all_vectored(bufs), ())
190     }
191
192     fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
193         handle_ebadf(self.0.write_fmt(fmt), ())
194     }
195 }
196
197 fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
198     match r {
199         Err(ref e) if stdio::is_ebadf(e) => Ok(default),
200         r => r,
201     }
202 }
203
204 /// A handle to the standard input stream of a process.
205 ///
206 /// Each handle is a shared reference to a global buffer of input data to this
207 /// process. A handle can be `lock`'d to gain full access to [`BufRead`] methods
208 /// (e.g., `.lines()`). Reads to this handle are otherwise locked with respect
209 /// to other reads.
210 ///
211 /// This handle implements the `Read` trait, but beware that concurrent reads
212 /// of `Stdin` must be executed with care.
213 ///
214 /// Created by the [`io::stdin`] method.
215 ///
216 /// [`io::stdin`]: stdin
217 ///
218 /// ### Note: Windows Portability Consideration
219 ///
220 /// When operating in a console, the Windows implementation of this stream does not support
221 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
222 /// an error.
223 ///
224 /// # Examples
225 ///
226 /// ```no_run
227 /// use std::io::{self, Read};
228 ///
229 /// fn main() -> io::Result<()> {
230 ///     let mut buffer = String::new();
231 ///     let mut stdin = io::stdin(); // We get `Stdin` here.
232 ///     stdin.read_to_string(&mut buffer)?;
233 ///     Ok(())
234 /// }
235 /// ```
236 #[stable(feature = "rust1", since = "1.0.0")]
237 pub struct Stdin {
238     inner: &'static Mutex<BufReader<StdinRaw>>,
239 }
240
241 /// A locked reference to the `Stdin` handle.
242 ///
243 /// This handle implements both the [`Read`] and [`BufRead`] traits, and
244 /// is constructed via the [`Stdin::lock`] method.
245 ///
246 /// ### Note: Windows Portability Consideration
247 ///
248 /// When operating in a console, the Windows implementation of this stream does not support
249 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
250 /// an error.
251 ///
252 /// # Examples
253 ///
254 /// ```no_run
255 /// use std::io::{self, Read};
256 ///
257 /// fn main() -> io::Result<()> {
258 ///     let mut buffer = String::new();
259 ///     let stdin = io::stdin(); // We get `Stdin` here.
260 ///     {
261 ///         let mut stdin_lock = stdin.lock(); // We get `StdinLock` here.
262 ///         stdin_lock.read_to_string(&mut buffer)?;
263 ///     } // `StdinLock` is dropped here.
264 ///     Ok(())
265 /// }
266 /// ```
267 #[stable(feature = "rust1", since = "1.0.0")]
268 pub struct StdinLock<'a> {
269     inner: MutexGuard<'a, BufReader<StdinRaw>>,
270 }
271
272 /// Constructs a new handle to the standard input of the current process.
273 ///
274 /// Each handle returned is a reference to a shared global buffer whose access
275 /// is synchronized via a mutex. If you need more explicit control over
276 /// locking, see the [`Stdin::lock`] method.
277 ///
278 /// ### Note: Windows Portability Consideration
279 /// When operating in a console, the Windows implementation of this stream does not support
280 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
281 /// an error.
282 ///
283 /// # Examples
284 ///
285 /// Using implicit synchronization:
286 ///
287 /// ```no_run
288 /// use std::io::{self, Read};
289 ///
290 /// fn main() -> io::Result<()> {
291 ///     let mut buffer = String::new();
292 ///     io::stdin().read_to_string(&mut buffer)?;
293 ///     Ok(())
294 /// }
295 /// ```
296 ///
297 /// Using explicit synchronization:
298 ///
299 /// ```no_run
300 /// use std::io::{self, Read};
301 ///
302 /// fn main() -> io::Result<()> {
303 ///     let mut buffer = String::new();
304 ///     let stdin = io::stdin();
305 ///     let mut handle = stdin.lock();
306 ///
307 ///     handle.read_to_string(&mut buffer)?;
308 ///     Ok(())
309 /// }
310 /// ```
311 #[stable(feature = "rust1", since = "1.0.0")]
312 pub fn stdin() -> Stdin {
313     static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
314     Stdin {
315         inner: INSTANCE.get_or_init(|| {
316             Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
317         }),
318     }
319 }
320
321 impl Stdin {
322     /// Locks this handle to the standard input stream, returning a readable
323     /// guard.
324     ///
325     /// The lock is released when the returned lock goes out of scope. The
326     /// returned guard also implements the [`Read`] and [`BufRead`] traits for
327     /// accessing the underlying data.
328     ///
329     /// # Examples
330     ///
331     /// ```no_run
332     /// use std::io::{self, Read};
333     ///
334     /// fn main() -> io::Result<()> {
335     ///     let mut buffer = String::new();
336     ///     let stdin = io::stdin();
337     ///     let mut handle = stdin.lock();
338     ///
339     ///     handle.read_to_string(&mut buffer)?;
340     ///     Ok(())
341     /// }
342     /// ```
343     #[stable(feature = "rust1", since = "1.0.0")]
344     pub fn lock(&self) -> StdinLock<'_> {
345         StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
346     }
347
348     /// Locks this handle and reads a line of input, appending it to the specified buffer.
349     ///
350     /// For detailed semantics of this method, see the documentation on
351     /// [`BufRead::read_line`].
352     ///
353     /// # Examples
354     ///
355     /// ```no_run
356     /// use std::io;
357     ///
358     /// let mut input = String::new();
359     /// match io::stdin().read_line(&mut input) {
360     ///     Ok(n) => {
361     ///         println!("{} bytes read", n);
362     ///         println!("{}", input);
363     ///     }
364     ///     Err(error) => println!("error: {}", error),
365     /// }
366     /// ```
367     ///
368     /// You can run the example one of two ways:
369     ///
370     /// - Pipe some text to it, e.g., `printf foo | path/to/executable`
371     /// - Give it text interactively by running the executable directly,
372     ///   in which case it will wait for the Enter key to be pressed before
373     ///   continuing
374     #[stable(feature = "rust1", since = "1.0.0")]
375     pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
376         self.lock().read_line(buf)
377     }
378 }
379
380 #[stable(feature = "std_debug", since = "1.16.0")]
381 impl fmt::Debug for Stdin {
382     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383         f.pad("Stdin { .. }")
384     }
385 }
386
387 #[stable(feature = "rust1", since = "1.0.0")]
388 impl Read for Stdin {
389     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
390         self.lock().read(buf)
391     }
392     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
393         self.lock().read_vectored(bufs)
394     }
395     #[inline]
396     fn is_read_vectored(&self) -> bool {
397         self.lock().is_read_vectored()
398     }
399     #[inline]
400     unsafe fn initializer(&self) -> Initializer {
401         Initializer::nop()
402     }
403     fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
404         self.lock().read_to_end(buf)
405     }
406     fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
407         self.lock().read_to_string(buf)
408     }
409     fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
410         self.lock().read_exact(buf)
411     }
412 }
413
414 #[stable(feature = "rust1", since = "1.0.0")]
415 impl Read for StdinLock<'_> {
416     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
417         self.inner.read(buf)
418     }
419
420     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
421         self.inner.read_vectored(bufs)
422     }
423
424     #[inline]
425     fn is_read_vectored(&self) -> bool {
426         self.inner.is_read_vectored()
427     }
428
429     #[inline]
430     unsafe fn initializer(&self) -> Initializer {
431         Initializer::nop()
432     }
433
434     fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
435         self.inner.read_to_end(buf)
436     }
437
438     fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
439         self.inner.read_to_string(buf)
440     }
441
442     fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
443         self.inner.read_exact(buf)
444     }
445 }
446
447 #[stable(feature = "rust1", since = "1.0.0")]
448 impl BufRead for StdinLock<'_> {
449     fn fill_buf(&mut self) -> io::Result<&[u8]> {
450         self.inner.fill_buf()
451     }
452
453     fn consume(&mut self, n: usize) {
454         self.inner.consume(n)
455     }
456
457     fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
458         self.inner.read_until(byte, buf)
459     }
460
461     fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
462         self.inner.read_line(buf)
463     }
464 }
465
466 #[stable(feature = "std_debug", since = "1.16.0")]
467 impl fmt::Debug for StdinLock<'_> {
468     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
469         f.pad("StdinLock { .. }")
470     }
471 }
472
473 /// A handle to the global standard output stream of the current process.
474 ///
475 /// Each handle shares a global buffer of data to be written to the standard
476 /// output stream. Access is also synchronized via a lock and explicit control
477 /// over locking is available via the [`lock`] method.
478 ///
479 /// Created by the [`io::stdout`] method.
480 ///
481 /// ### Note: Windows Portability Consideration
482 /// When operating in a console, the Windows implementation of this stream does not support
483 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
484 /// an error.
485 ///
486 /// [`lock`]: Stdout::lock
487 /// [`io::stdout`]: stdout
488 #[stable(feature = "rust1", since = "1.0.0")]
489 pub struct Stdout {
490     // FIXME: this should be LineWriter or BufWriter depending on the state of
491     //        stdout (tty or not). Note that if this is not line buffered it
492     //        should also flush-on-panic or some form of flush-on-abort.
493     inner: &'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>,
494 }
495
496 /// A locked reference to the `Stdout` handle.
497 ///
498 /// This handle implements the [`Write`] trait, and is constructed via
499 /// the [`Stdout::lock`] method.
500 ///
501 /// ### Note: Windows Portability Consideration
502 /// When operating in a console, the Windows implementation of this stream does not support
503 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
504 /// an error.
505 #[stable(feature = "rust1", since = "1.0.0")]
506 pub struct StdoutLock<'a> {
507     inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
508 }
509
510 /// Constructs a new handle to the standard output of the current process.
511 ///
512 /// Each handle returned is a reference to a shared global buffer whose access
513 /// is synchronized via a mutex. If you need more explicit control over
514 /// locking, see the [`Stdout::lock`] method.
515 ///
516 /// ### Note: Windows Portability Consideration
517 /// When operating in a console, the Windows implementation of this stream does not support
518 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
519 /// an error.
520 ///
521 /// # Examples
522 ///
523 /// Using implicit synchronization:
524 ///
525 /// ```no_run
526 /// use std::io::{self, Write};
527 ///
528 /// fn main() -> io::Result<()> {
529 ///     io::stdout().write_all(b"hello world")?;
530 ///
531 ///     Ok(())
532 /// }
533 /// ```
534 ///
535 /// Using explicit synchronization:
536 ///
537 /// ```no_run
538 /// use std::io::{self, Write};
539 ///
540 /// fn main() -> io::Result<()> {
541 ///     let stdout = io::stdout();
542 ///     let mut handle = stdout.lock();
543 ///
544 ///     handle.write_all(b"hello world")?;
545 ///
546 ///     Ok(())
547 /// }
548 /// ```
549 #[stable(feature = "rust1", since = "1.0.0")]
550 pub fn stdout() -> Stdout {
551     static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> =
552         SyncOnceCell::new();
553     Stdout {
554         inner: INSTANCE.get_or_init(|| unsafe {
555             let _ = sys_common::at_exit(|| {
556                 if let Some(instance) = INSTANCE.get() {
557                     // Flush the data and disable buffering during shutdown
558                     // by replacing the line writer by one with zero
559                     // buffering capacity.
560                     // We use try_lock() instead of lock(), because someone
561                     // might have leaked a StdoutLock, which would
562                     // otherwise cause a deadlock here.
563                     if let Some(lock) = instance.try_lock() {
564                         *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
565                     }
566                 }
567             });
568             let r = ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw())));
569             r.init();
570             r
571         }),
572     }
573 }
574
575 impl Stdout {
576     /// Locks this handle to the standard output stream, returning a writable
577     /// guard.
578     ///
579     /// The lock is released when the returned lock goes out of scope. The
580     /// returned guard also implements the `Write` trait for writing data.
581     ///
582     /// # Examples
583     ///
584     /// ```no_run
585     /// use std::io::{self, Write};
586     ///
587     /// fn main() -> io::Result<()> {
588     ///     let stdout = io::stdout();
589     ///     let mut handle = stdout.lock();
590     ///
591     ///     handle.write_all(b"hello world")?;
592     ///
593     ///     Ok(())
594     /// }
595     /// ```
596     #[stable(feature = "rust1", since = "1.0.0")]
597     pub fn lock(&self) -> StdoutLock<'_> {
598         StdoutLock { inner: self.inner.lock() }
599     }
600 }
601
602 #[stable(feature = "std_debug", since = "1.16.0")]
603 impl fmt::Debug for Stdout {
604     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
605         f.pad("Stdout { .. }")
606     }
607 }
608
609 #[stable(feature = "rust1", since = "1.0.0")]
610 impl Write for Stdout {
611     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
612         (&*self).write(buf)
613     }
614     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
615         (&*self).write_vectored(bufs)
616     }
617     #[inline]
618     fn is_write_vectored(&self) -> bool {
619         io::Write::is_write_vectored(&&*self)
620     }
621     fn flush(&mut self) -> io::Result<()> {
622         (&*self).flush()
623     }
624     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
625         (&*self).write_all(buf)
626     }
627     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
628         (&*self).write_all_vectored(bufs)
629     }
630     fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
631         (&*self).write_fmt(args)
632     }
633 }
634
635 #[stable(feature = "write_mt", since = "1.48.0")]
636 impl Write for &Stdout {
637     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
638         self.lock().write(buf)
639     }
640     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
641         self.lock().write_vectored(bufs)
642     }
643     #[inline]
644     fn is_write_vectored(&self) -> bool {
645         self.lock().is_write_vectored()
646     }
647     fn flush(&mut self) -> io::Result<()> {
648         self.lock().flush()
649     }
650     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
651         self.lock().write_all(buf)
652     }
653     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
654         self.lock().write_all_vectored(bufs)
655     }
656     fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
657         self.lock().write_fmt(args)
658     }
659 }
660
661 #[stable(feature = "rust1", since = "1.0.0")]
662 impl Write for StdoutLock<'_> {
663     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
664         self.inner.borrow_mut().write(buf)
665     }
666     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
667         self.inner.borrow_mut().write_vectored(bufs)
668     }
669     #[inline]
670     fn is_write_vectored(&self) -> bool {
671         self.inner.borrow_mut().is_write_vectored()
672     }
673     fn flush(&mut self) -> io::Result<()> {
674         self.inner.borrow_mut().flush()
675     }
676     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
677         self.inner.borrow_mut().write_all(buf)
678     }
679     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
680         self.inner.borrow_mut().write_all_vectored(bufs)
681     }
682 }
683
684 #[stable(feature = "std_debug", since = "1.16.0")]
685 impl fmt::Debug for StdoutLock<'_> {
686     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
687         f.pad("StdoutLock { .. }")
688     }
689 }
690
691 /// A handle to the standard error stream of a process.
692 ///
693 /// For more information, see the [`io::stderr`] method.
694 ///
695 /// [`io::stderr`]: stderr
696 ///
697 /// ### Note: Windows Portability Consideration
698 /// When operating in a console, the Windows implementation of this stream does not support
699 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
700 /// an error.
701 #[stable(feature = "rust1", since = "1.0.0")]
702 pub struct Stderr {
703     inner: &'static ReentrantMutex<RefCell<StderrRaw>>,
704 }
705
706 /// A locked reference to the `Stderr` handle.
707 ///
708 /// This handle implements the `Write` trait and is constructed via
709 /// the [`Stderr::lock`] method.
710 ///
711 /// ### Note: Windows Portability Consideration
712 /// When operating in a console, the Windows implementation of this stream does not support
713 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
714 /// an error.
715 #[stable(feature = "rust1", since = "1.0.0")]
716 pub struct StderrLock<'a> {
717     inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
718 }
719
720 /// Constructs a new handle to the standard error of the current process.
721 ///
722 /// This handle is not buffered.
723 ///
724 /// ### Note: Windows Portability Consideration
725 /// When operating in a console, the Windows implementation of this stream does not support
726 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
727 /// an error.
728 ///
729 /// # Examples
730 ///
731 /// Using implicit synchronization:
732 ///
733 /// ```no_run
734 /// use std::io::{self, Write};
735 ///
736 /// fn main() -> io::Result<()> {
737 ///     io::stderr().write_all(b"hello world")?;
738 ///
739 ///     Ok(())
740 /// }
741 /// ```
742 ///
743 /// Using explicit synchronization:
744 ///
745 /// ```no_run
746 /// use std::io::{self, Write};
747 ///
748 /// fn main() -> io::Result<()> {
749 ///     let stderr = io::stderr();
750 ///     let mut handle = stderr.lock();
751 ///
752 ///     handle.write_all(b"hello world")?;
753 ///
754 ///     Ok(())
755 /// }
756 /// ```
757 #[stable(feature = "rust1", since = "1.0.0")]
758 pub fn stderr() -> Stderr {
759     // Note that unlike `stdout()` we don't use `Lazy` here which registers a
760     // destructor. Stderr is not buffered nor does the `stderr_raw` type consume
761     // any owned resources, so there's no need to run any destructors at some
762     // point in the future.
763     //
764     // This has the added benefit of allowing `stderr` to be usable during
765     // process shutdown as well!
766     static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<StderrRaw>>> = SyncOnceCell::new();
767
768     Stderr {
769         inner: INSTANCE.get_or_init(|| unsafe {
770             let r = ReentrantMutex::new(RefCell::new(stderr_raw()));
771             r.init();
772             r
773         }),
774     }
775 }
776
777 impl Stderr {
778     /// Locks this handle to the standard error stream, returning a writable
779     /// guard.
780     ///
781     /// The lock is released when the returned lock goes out of scope. The
782     /// returned guard also implements the [`Write`] trait for writing data.
783     ///
784     /// # Examples
785     ///
786     /// ```
787     /// use std::io::{self, Write};
788     ///
789     /// fn foo() -> io::Result<()> {
790     ///     let stderr = io::stderr();
791     ///     let mut handle = stderr.lock();
792     ///
793     ///     handle.write_all(b"hello world")?;
794     ///
795     ///     Ok(())
796     /// }
797     /// ```
798     #[stable(feature = "rust1", since = "1.0.0")]
799     pub fn lock(&self) -> StderrLock<'_> {
800         StderrLock { inner: self.inner.lock() }
801     }
802 }
803
804 #[stable(feature = "std_debug", since = "1.16.0")]
805 impl fmt::Debug for Stderr {
806     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
807         f.pad("Stderr { .. }")
808     }
809 }
810
811 #[stable(feature = "rust1", since = "1.0.0")]
812 impl Write for Stderr {
813     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
814         (&*self).write(buf)
815     }
816     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
817         (&*self).write_vectored(bufs)
818     }
819     #[inline]
820     fn is_write_vectored(&self) -> bool {
821         io::Write::is_write_vectored(&&*self)
822     }
823     fn flush(&mut self) -> io::Result<()> {
824         (&*self).flush()
825     }
826     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
827         (&*self).write_all(buf)
828     }
829     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
830         (&*self).write_all_vectored(bufs)
831     }
832     fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
833         (&*self).write_fmt(args)
834     }
835 }
836
837 #[stable(feature = "write_mt", since = "1.48.0")]
838 impl Write for &Stderr {
839     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
840         self.lock().write(buf)
841     }
842     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
843         self.lock().write_vectored(bufs)
844     }
845     #[inline]
846     fn is_write_vectored(&self) -> bool {
847         self.lock().is_write_vectored()
848     }
849     fn flush(&mut self) -> io::Result<()> {
850         self.lock().flush()
851     }
852     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
853         self.lock().write_all(buf)
854     }
855     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
856         self.lock().write_all_vectored(bufs)
857     }
858     fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
859         self.lock().write_fmt(args)
860     }
861 }
862
863 #[stable(feature = "rust1", since = "1.0.0")]
864 impl Write for StderrLock<'_> {
865     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
866         self.inner.borrow_mut().write(buf)
867     }
868     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
869         self.inner.borrow_mut().write_vectored(bufs)
870     }
871     #[inline]
872     fn is_write_vectored(&self) -> bool {
873         self.inner.borrow_mut().is_write_vectored()
874     }
875     fn flush(&mut self) -> io::Result<()> {
876         self.inner.borrow_mut().flush()
877     }
878     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
879         self.inner.borrow_mut().write_all(buf)
880     }
881     fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
882         self.inner.borrow_mut().write_all_vectored(bufs)
883     }
884 }
885
886 #[stable(feature = "std_debug", since = "1.16.0")]
887 impl fmt::Debug for StderrLock<'_> {
888     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
889         f.pad("StderrLock { .. }")
890     }
891 }
892
893 /// Resets the thread-local stderr handle to the specified writer
894 ///
895 /// This will replace the current thread's stderr handle, returning the old
896 /// handle. All future calls to `panic!` and friends will emit their output to
897 /// this specified handle.
898 ///
899 /// Note that this does not need to be called for all new threads; the default
900 /// output handle is to the process's stderr stream.
901 #[unstable(
902     feature = "set_stdio",
903     reason = "this function may disappear completely or be replaced \
904                      with a more general mechanism",
905     issue = "none"
906 )]
907 #[doc(hidden)]
908 pub fn set_panic(sink: Option<LocalStream>) -> Option<LocalStream> {
909     if sink.is_none() && !LOCAL_STREAMS.load(Ordering::Relaxed) {
910         // LOCAL_STDERR is definitely None since LOCAL_STREAMS is false.
911         return None;
912     }
913     LOCAL_STREAMS.store(true, Ordering::Relaxed);
914     LOCAL_STDERR.with(move |slot| slot.replace(sink))
915 }
916
917 /// Resets the thread-local stdout handle to the specified writer
918 ///
919 /// This will replace the current thread's stdout handle, returning the old
920 /// handle. All future calls to `print!` and friends will emit their output to
921 /// this specified handle.
922 ///
923 /// Note that this does not need to be called for all new threads; the default
924 /// output handle is to the process's stdout stream.
925 #[unstable(
926     feature = "set_stdio",
927     reason = "this function may disappear completely or be replaced \
928                      with a more general mechanism",
929     issue = "none"
930 )]
931 #[doc(hidden)]
932 pub fn set_print(sink: Option<LocalStream>) -> Option<LocalStream> {
933     if sink.is_none() && !LOCAL_STREAMS.load(Ordering::Relaxed) {
934         // LOCAL_STDOUT is definitely None since LOCAL_STREAMS is false.
935         return None;
936     }
937     LOCAL_STREAMS.store(true, Ordering::Relaxed);
938     LOCAL_STDOUT.with(move |slot| slot.replace(sink))
939 }
940
941 pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
942     // Don't waste time when LOCAL_{STDOUT,STDERR} are definitely None.
943     if !LOCAL_STREAMS.load(Ordering::Relaxed) {
944         return (None, None);
945     }
946
947     let clone = |cell: &Cell<Option<LocalStream>>| {
948         let s = cell.take();
949         cell.set(s.clone());
950         s
951     };
952
953     (LOCAL_STDOUT.with(clone), LOCAL_STDERR.with(clone))
954 }
955
956 /// Write `args` to output stream `local_s` if possible, `global_s`
957 /// otherwise. `label` identifies the stream in a panic message.
958 ///
959 /// This function is used to print error messages, so it takes extra
960 /// care to avoid causing a panic when `local_s` is unusable.
961 /// For instance, if the TLS key for the local stream is
962 /// already destroyed, or if the local stream is locked by another
963 /// thread, it will just fall back to the global stream.
964 ///
965 /// However, if the actual I/O causes an error, this function does panic.
966 fn print_to<T>(
967     args: fmt::Arguments<'_>,
968     local_s: &'static LocalKey<Cell<Option<LocalStream>>>,
969     global_s: fn() -> T,
970     label: &str,
971 ) where
972     T: Write,
973 {
974     if LOCAL_STREAMS.load(Ordering::Relaxed)
975         && local_s.try_with(|s| {
976             // Note that we completely remove a local sink to write to in case
977             // our printing recursively panics/prints, so the recursive
978             // panic/print goes to the global sink instead of our local sink.
979             s.take().map(|w| {
980                 let _ = w.lock().unwrap_or_else(|e| e.into_inner()).write_fmt(args);
981                 s.set(Some(w));
982             })
983         }) == Ok(Some(()))
984     {
985         // Succesfully wrote to local stream.
986         return;
987     }
988
989     if let Err(e) = global_s().write_fmt(args) {
990         panic!("failed printing to {}: {}", label, e);
991     }
992 }
993
994 #[unstable(
995     feature = "print_internals",
996     reason = "implementation detail which may disappear or be replaced at any time",
997     issue = "none"
998 )]
999 #[doc(hidden)]
1000 #[cfg(not(test))]
1001 pub fn _print(args: fmt::Arguments<'_>) {
1002     print_to(args, &LOCAL_STDOUT, stdout, "stdout");
1003 }
1004
1005 #[unstable(
1006     feature = "print_internals",
1007     reason = "implementation detail which may disappear or be replaced at any time",
1008     issue = "none"
1009 )]
1010 #[doc(hidden)]
1011 #[cfg(not(test))]
1012 pub fn _eprint(args: fmt::Arguments<'_>) {
1013     print_to(args, &LOCAL_STDERR, stderr, "stderr");
1014 }
1015
1016 #[cfg(test)]
1017 pub use realstd::io::{_eprint, _print};