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