1 #![cfg_attr(test, allow(unused))]
6 use crate::io::prelude::*;
8 use crate::cell::{Cell, RefCell};
10 use crate::io::{self, BufReader, IoSlice, IoSliceMut, LineWriter, Lines};
11 use crate::lazy::SyncOnceCell;
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};
18 type LocalStream = Arc<Mutex<Vec<u8>>>;
21 /// Used by the test crate to capture the output of the print macros and panics.
22 static OUTPUT_CAPTURE: Cell<Option<LocalStream>> = {
27 /// Flag to indicate OUTPUT_CAPTURE is used.
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.
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);
41 /// A handle to a raw instance of the standard input stream of this process.
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);
47 /// A handle to a raw instance of the standard output stream of this process.
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);
53 /// A handle to a raw instance of the standard output stream of this process.
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);
59 /// Constructs a new raw handle to the standard input of this process.
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.
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())
71 /// Constructs a new raw handle to the standard output stream of this process.
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.
78 /// The returned handle has no external synchronization or buffering layered on
80 #[unstable(feature = "libstd_sys_internals", issue = "none")]
81 const fn stdout_raw() -> StdoutRaw {
82 StdoutRaw(stdio::Stdout::new())
85 /// Constructs a new raw handle to the standard error stream of this process.
87 /// The returned handle does not interact with any other handles created nor
88 /// handles returned by `std::io::stderr`.
90 /// The returned handle has no external synchronization or buffering layered on
92 #[unstable(feature = "libstd_sys_internals", issue = "none")]
93 const fn stderr_raw() -> StderrRaw {
94 StderrRaw(stdio::Stderr::new())
97 impl Read for StdinRaw {
98 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
99 handle_ebadf(self.0.read(buf), 0)
102 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
103 handle_ebadf(self.0.read_vectored(bufs), 0)
107 fn is_read_vectored(&self) -> bool {
108 self.0.is_read_vectored()
111 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
112 handle_ebadf(self.0.read_to_end(buf), 0)
115 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
116 handle_ebadf(self.0.read_to_string(buf), 0)
120 impl Write for StdoutRaw {
121 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
122 handle_ebadf(self.0.write(buf), buf.len())
125 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
126 let total = bufs.iter().map(|b| b.len()).sum();
127 handle_ebadf(self.0.write_vectored(bufs), total)
131 fn is_write_vectored(&self) -> bool {
132 self.0.is_write_vectored()
135 fn flush(&mut self) -> io::Result<()> {
136 handle_ebadf(self.0.flush(), ())
139 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
140 handle_ebadf(self.0.write_all(buf), ())
143 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
144 handle_ebadf(self.0.write_all_vectored(bufs), ())
147 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
148 handle_ebadf(self.0.write_fmt(fmt), ())
152 impl Write for StderrRaw {
153 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
154 handle_ebadf(self.0.write(buf), buf.len())
157 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
158 let total = bufs.iter().map(|b| b.len()).sum();
159 handle_ebadf(self.0.write_vectored(bufs), total)
163 fn is_write_vectored(&self) -> bool {
164 self.0.is_write_vectored()
167 fn flush(&mut self) -> io::Result<()> {
168 handle_ebadf(self.0.flush(), ())
171 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
172 handle_ebadf(self.0.write_all(buf), ())
175 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
176 handle_ebadf(self.0.write_all_vectored(bufs), ())
179 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
180 handle_ebadf(self.0.write_fmt(fmt), ())
184 fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
186 Err(ref e) if stdio::is_ebadf(e) => Ok(default),
191 /// A handle to the standard input stream of a process.
193 /// Each handle is a shared reference to a global buffer of input data to this
194 /// process. A handle can be `lock`'d to gain full access to [`BufRead`] methods
195 /// (e.g., `.lines()`). Reads to this handle are otherwise locked with respect
198 /// This handle implements the `Read` trait, but beware that concurrent reads
199 /// of `Stdin` must be executed with care.
201 /// Created by the [`io::stdin`] method.
203 /// [`io::stdin`]: stdin
205 /// ### Note: Windows Portability Consideration
207 /// When operating in a console, the Windows implementation of this stream does not support
208 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
216 /// fn main() -> io::Result<()> {
217 /// let mut buffer = String::new();
218 /// let mut stdin = io::stdin(); // We get `Stdin` here.
219 /// stdin.read_line(&mut buffer)?;
223 #[stable(feature = "rust1", since = "1.0.0")]
225 inner: &'static Mutex<BufReader<StdinRaw>>,
228 /// A locked reference to the [`Stdin`] handle.
230 /// This handle implements both the [`Read`] and [`BufRead`] traits, and
231 /// is constructed via the [`Stdin::lock`] method.
233 /// ### Note: Windows Portability Consideration
235 /// When operating in a console, the Windows implementation of this stream does not support
236 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
242 /// use std::io::{self, BufRead};
244 /// fn main() -> io::Result<()> {
245 /// let mut buffer = String::new();
246 /// let stdin = io::stdin(); // We get `Stdin` here.
248 /// let mut handle = stdin.lock(); // We get `StdinLock` here.
249 /// handle.read_line(&mut buffer)?;
250 /// } // `StdinLock` is dropped here.
254 #[must_use = "if unused stdin will immediately unlock"]
255 #[stable(feature = "rust1", since = "1.0.0")]
256 pub struct StdinLock<'a> {
257 inner: MutexGuard<'a, BufReader<StdinRaw>>,
260 /// Constructs a new handle to the standard input of the current process.
262 /// Each handle returned is a reference to a shared global buffer whose access
263 /// is synchronized via a mutex. If you need more explicit control over
264 /// locking, see the [`Stdin::lock`] method.
266 /// ### Note: Windows Portability Consideration
267 /// When operating in a console, the Windows implementation of this stream does not support
268 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
273 /// Using implicit synchronization:
278 /// fn main() -> io::Result<()> {
279 /// let mut buffer = String::new();
280 /// io::stdin().read_line(&mut buffer)?;
285 /// Using explicit synchronization:
288 /// use std::io::{self, BufRead};
290 /// fn main() -> io::Result<()> {
291 /// let mut buffer = String::new();
292 /// let stdin = io::stdin();
293 /// let mut handle = stdin.lock();
295 /// handle.read_line(&mut buffer)?;
300 #[stable(feature = "rust1", since = "1.0.0")]
301 pub fn stdin() -> Stdin {
302 static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
304 inner: INSTANCE.get_or_init(|| {
305 Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
310 /// Constructs a new locked handle to the standard input of the current
313 /// Each handle returned is a guard granting locked access to a shared
314 /// global buffer whose access is synchronized via a mutex. If you need
315 /// more explicit control over locking, for example, in a multi-threaded
316 /// program, use the [`io::stdin`] function to obtain an unlocked handle,
317 /// along with the [`Stdin::lock`] method.
319 /// The lock is released when the returned guard goes out of scope. The
320 /// returned guard also implements the [`Read`] and [`BufRead`] traits for
321 /// accessing the underlying data.
323 /// **Note**: The mutex locked by this handle is not reentrant. Even in a
324 /// single-threaded program, calling other code that accesses [`Stdin`]
325 /// could cause a deadlock or panic, if this locked handle is held across
328 /// ### Note: Windows Portability Consideration
329 /// When operating in a console, the Windows implementation of this stream does not support
330 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
336 /// #![feature(stdio_locked)]
337 /// use std::io::{self, BufRead};
339 /// fn main() -> io::Result<()> {
340 /// let mut buffer = String::new();
341 /// let mut handle = io::stdin_locked();
343 /// handle.read_line(&mut buffer)?;
347 #[unstable(feature = "stdio_locked", issue = "86845")]
348 pub fn stdin_locked() -> StdinLock<'static> {
349 stdin().into_locked()
353 /// Locks this handle to the standard input stream, returning a readable
356 /// The lock is released when the returned lock goes out of scope. The
357 /// returned guard also implements the [`Read`] and [`BufRead`] traits for
358 /// accessing the underlying data.
363 /// use std::io::{self, BufRead};
365 /// fn main() -> io::Result<()> {
366 /// let mut buffer = String::new();
367 /// let stdin = io::stdin();
368 /// let mut handle = stdin.lock();
370 /// handle.read_line(&mut buffer)?;
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub fn lock(&self) -> StdinLock<'_> {
379 /// Locks this handle and reads a line of input, appending it to the specified buffer.
381 /// For detailed semantics of this method, see the documentation on
382 /// [`BufRead::read_line`].
389 /// let mut input = String::new();
390 /// match io::stdin().read_line(&mut input) {
392 /// println!("{} bytes read", n);
393 /// println!("{}", input);
395 /// Err(error) => println!("error: {}", error),
399 /// You can run the example one of two ways:
401 /// - Pipe some text to it, e.g., `printf foo | path/to/executable`
402 /// - Give it text interactively by running the executable directly,
403 /// in which case it will wait for the Enter key to be pressed before
405 #[stable(feature = "rust1", since = "1.0.0")]
406 pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
407 self.lock().read_line(buf)
410 // Locks this handle with any lifetime. This depends on the
411 // implementation detail that the underlying `Mutex` is static.
412 fn lock_any<'a>(&self) -> StdinLock<'a> {
413 StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
416 /// Consumes this handle to the standard input stream, locking the
417 /// shared global buffer associated with the stream and returning a
420 /// The lock is released when the returned guard goes out of scope. The
421 /// returned guard also implements the [`Read`] and [`BufRead`] traits
422 /// for accessing the underlying data.
424 /// It is often simpler to directly get a locked handle using the
425 /// [`stdin_locked`] function instead, unless nearby code also needs to
426 /// use an unlocked handle.
431 /// #![feature(stdio_locked)]
432 /// use std::io::{self, BufRead};
434 /// fn main() -> io::Result<()> {
435 /// let mut buffer = String::new();
436 /// let mut handle = io::stdin().into_locked();
438 /// handle.read_line(&mut buffer)?;
442 #[unstable(feature = "stdio_locked", issue = "86845")]
443 pub fn into_locked(self) -> StdinLock<'static> {
447 /// Consumes this handle and returns an iterator over input lines.
449 /// For detailed semantics of this method, see the documentation on
450 /// [`BufRead::lines`].
455 /// #![feature(stdin_forwarders)]
458 /// let lines = io::stdin().lines();
459 /// for line in lines {
460 /// println!("got a line: {}", line.unwrap());
463 #[must_use = "`self` will be dropped if the result is not used"]
464 #[unstable(feature = "stdin_forwarders", issue = "87096")]
465 pub fn lines(self) -> Lines<StdinLock<'static>> {
466 self.into_locked().lines()
470 #[stable(feature = "std_debug", since = "1.16.0")]
471 impl fmt::Debug for Stdin {
472 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
473 f.debug_struct("Stdin").finish_non_exhaustive()
477 #[stable(feature = "rust1", since = "1.0.0")]
478 impl Read for Stdin {
479 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
480 self.lock().read(buf)
482 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
483 self.lock().read_vectored(bufs)
486 fn is_read_vectored(&self) -> bool {
487 self.lock().is_read_vectored()
489 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
490 self.lock().read_to_end(buf)
492 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
493 self.lock().read_to_string(buf)
495 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
496 self.lock().read_exact(buf)
500 // only used by platform-dependent io::copy specializations, i.e. unused on some platforms
501 #[cfg(any(target_os = "linux", target_os = "android"))]
503 pub(crate) fn as_mut_buf(&mut self) -> &mut BufReader<impl Read> {
508 #[stable(feature = "rust1", since = "1.0.0")]
509 impl Read for StdinLock<'_> {
510 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
514 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
515 self.inner.read_vectored(bufs)
519 fn is_read_vectored(&self) -> bool {
520 self.inner.is_read_vectored()
523 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
524 self.inner.read_to_end(buf)
527 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
528 self.inner.read_to_string(buf)
531 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
532 self.inner.read_exact(buf)
536 #[stable(feature = "rust1", since = "1.0.0")]
537 impl BufRead for StdinLock<'_> {
538 fn fill_buf(&mut self) -> io::Result<&[u8]> {
539 self.inner.fill_buf()
542 fn consume(&mut self, n: usize) {
543 self.inner.consume(n)
546 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
547 self.inner.read_until(byte, buf)
550 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
551 self.inner.read_line(buf)
555 #[stable(feature = "std_debug", since = "1.16.0")]
556 impl fmt::Debug for StdinLock<'_> {
557 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
558 f.debug_struct("StdinLock").finish_non_exhaustive()
562 /// A handle to the global standard output stream of the current process.
564 /// Each handle shares a global buffer of data to be written to the standard
565 /// output stream. Access is also synchronized via a lock and explicit control
566 /// over locking is available via the [`lock`] method.
568 /// Created by the [`io::stdout`] method.
570 /// ### Note: Windows Portability Consideration
571 /// When operating in a console, the Windows implementation of this stream does not support
572 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
575 /// [`lock`]: Stdout::lock
576 /// [`io::stdout`]: stdout
577 #[stable(feature = "rust1", since = "1.0.0")]
579 // FIXME: this should be LineWriter or BufWriter depending on the state of
580 // stdout (tty or not). Note that if this is not line buffered it
581 // should also flush-on-panic or some form of flush-on-abort.
582 inner: Pin<&'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
585 /// A locked reference to the [`Stdout`] handle.
587 /// This handle implements the [`Write`] trait, and is constructed via
588 /// the [`Stdout::lock`] method. See its documentation for more.
590 /// ### Note: Windows Portability Consideration
591 /// When operating in a console, the Windows implementation of this stream does not support
592 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
594 #[must_use = "if unused stdout will immediately unlock"]
595 #[stable(feature = "rust1", since = "1.0.0")]
596 pub struct StdoutLock<'a> {
597 inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
600 static STDOUT: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = SyncOnceCell::new();
602 /// Constructs a new handle to the standard output of the current process.
604 /// Each handle returned is a reference to a shared global buffer whose access
605 /// is synchronized via a mutex. If you need more explicit control over
606 /// locking, see the [`Stdout::lock`] method.
608 /// ### Note: Windows Portability Consideration
609 /// When operating in a console, the Windows implementation of this stream does not support
610 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
615 /// Using implicit synchronization:
618 /// use std::io::{self, Write};
620 /// fn main() -> io::Result<()> {
621 /// io::stdout().write_all(b"hello world")?;
627 /// Using explicit synchronization:
630 /// use std::io::{self, Write};
632 /// fn main() -> io::Result<()> {
633 /// let stdout = io::stdout();
634 /// let mut handle = stdout.lock();
636 /// handle.write_all(b"hello world")?;
642 #[stable(feature = "rust1", since = "1.0.0")]
643 pub fn stdout() -> Stdout {
645 inner: Pin::static_ref(&STDOUT).get_or_init_pin(
646 || unsafe { ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw()))) },
647 |mutex| unsafe { mutex.init() },
652 /// Constructs a new locked handle to the standard output of the current
655 /// Each handle returned is a guard granting locked access to a shared
656 /// global buffer whose access is synchronized via a mutex. If you need
657 /// more explicit control over locking, for example, in a multi-threaded
658 /// program, use the [`io::stdout`] function to obtain an unlocked handle,
659 /// along with the [`Stdout::lock`] method.
661 /// The lock is released when the returned guard goes out of scope. The
662 /// returned guard also implements the [`Write`] trait for writing data.
664 /// ### Note: Windows Portability Consideration
665 /// When operating in a console, the Windows implementation of this stream does not support
666 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
672 /// #![feature(stdio_locked)]
673 /// use std::io::{self, Write};
675 /// fn main() -> io::Result<()> {
676 /// let mut handle = io::stdout_locked();
678 /// handle.write_all(b"hello world")?;
683 #[unstable(feature = "stdio_locked", issue = "86845")]
684 pub fn stdout_locked() -> StdoutLock<'static> {
685 stdout().into_locked()
689 if let Some(instance) = STDOUT.get() {
690 // Flush the data and disable buffering during shutdown
691 // by replacing the line writer by one with zero
692 // buffering capacity.
693 // We use try_lock() instead of lock(), because someone
694 // might have leaked a StdoutLock, which would
695 // otherwise cause a deadlock here.
696 if let Some(lock) = Pin::static_ref(instance).try_lock() {
697 *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
703 /// Locks this handle to the standard output stream, returning a writable
706 /// The lock is released when the returned lock goes out of scope. The
707 /// returned guard also implements the `Write` trait for writing data.
712 /// use std::io::{self, Write};
714 /// fn main() -> io::Result<()> {
715 /// let stdout = io::stdout();
716 /// let mut handle = stdout.lock();
718 /// handle.write_all(b"hello world")?;
723 #[stable(feature = "rust1", since = "1.0.0")]
724 pub fn lock(&self) -> StdoutLock<'_> {
728 // Locks this handle with any lifetime. This depends on the
729 // implementation detail that the underlying `ReentrantMutex` is
731 fn lock_any<'a>(&self) -> StdoutLock<'a> {
732 StdoutLock { inner: self.inner.lock() }
735 /// Consumes this handle to the standard output stream, locking the
736 /// shared global buffer associated with the stream and returning a
739 /// The lock is released when the returned lock goes out of scope. The
740 /// returned guard also implements the [`Write`] trait for writing data.
742 /// It is often simpler to directly get a locked handle using the
743 /// [`io::stdout_locked`] function instead, unless nearby code also
744 /// needs to use an unlocked handle.
749 /// #![feature(stdio_locked)]
750 /// use std::io::{self, Write};
752 /// fn main() -> io::Result<()> {
753 /// let mut handle = io::stdout().into_locked();
755 /// handle.write_all(b"hello world")?;
760 #[unstable(feature = "stdio_locked", issue = "86845")]
761 pub fn into_locked(self) -> StdoutLock<'static> {
766 #[stable(feature = "std_debug", since = "1.16.0")]
767 impl fmt::Debug for Stdout {
768 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
769 f.debug_struct("Stdout").finish_non_exhaustive()
773 #[stable(feature = "rust1", since = "1.0.0")]
774 impl Write for Stdout {
775 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
778 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
779 (&*self).write_vectored(bufs)
782 fn is_write_vectored(&self) -> bool {
783 io::Write::is_write_vectored(&&*self)
785 fn flush(&mut self) -> io::Result<()> {
788 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
789 (&*self).write_all(buf)
791 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
792 (&*self).write_all_vectored(bufs)
794 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
795 (&*self).write_fmt(args)
799 #[stable(feature = "write_mt", since = "1.48.0")]
800 impl Write for &Stdout {
801 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
802 self.lock().write(buf)
804 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
805 self.lock().write_vectored(bufs)
808 fn is_write_vectored(&self) -> bool {
809 self.lock().is_write_vectored()
811 fn flush(&mut self) -> io::Result<()> {
814 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
815 self.lock().write_all(buf)
817 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
818 self.lock().write_all_vectored(bufs)
820 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
821 self.lock().write_fmt(args)
825 #[stable(feature = "rust1", since = "1.0.0")]
826 impl Write for StdoutLock<'_> {
827 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
828 self.inner.borrow_mut().write(buf)
830 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
831 self.inner.borrow_mut().write_vectored(bufs)
834 fn is_write_vectored(&self) -> bool {
835 self.inner.borrow_mut().is_write_vectored()
837 fn flush(&mut self) -> io::Result<()> {
838 self.inner.borrow_mut().flush()
840 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
841 self.inner.borrow_mut().write_all(buf)
843 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
844 self.inner.borrow_mut().write_all_vectored(bufs)
848 #[stable(feature = "std_debug", since = "1.16.0")]
849 impl fmt::Debug for StdoutLock<'_> {
850 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
851 f.debug_struct("StdoutLock").finish_non_exhaustive()
855 /// A handle to the standard error stream of a process.
857 /// For more information, see the [`io::stderr`] method.
859 /// [`io::stderr`]: stderr
861 /// ### Note: Windows Portability Consideration
862 /// When operating in a console, the Windows implementation of this stream does not support
863 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
865 #[stable(feature = "rust1", since = "1.0.0")]
867 inner: Pin<&'static ReentrantMutex<RefCell<StderrRaw>>>,
870 /// A locked reference to the [`Stderr`] handle.
872 /// This handle implements the [`Write`] trait and is constructed via
873 /// the [`Stderr::lock`] method. See its documentation for more.
875 /// ### Note: Windows Portability Consideration
876 /// When operating in a console, the Windows implementation of this stream does not support
877 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
879 #[must_use = "if unused stderr will immediately unlock"]
880 #[stable(feature = "rust1", since = "1.0.0")]
881 pub struct StderrLock<'a> {
882 inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
885 /// Constructs a new handle to the standard error of the current process.
887 /// This handle is not buffered.
889 /// ### Note: Windows Portability Consideration
890 /// When operating in a console, the Windows implementation of this stream does not support
891 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
896 /// Using implicit synchronization:
899 /// use std::io::{self, Write};
901 /// fn main() -> io::Result<()> {
902 /// io::stderr().write_all(b"hello world")?;
908 /// Using explicit synchronization:
911 /// use std::io::{self, Write};
913 /// fn main() -> io::Result<()> {
914 /// let stderr = io::stderr();
915 /// let mut handle = stderr.lock();
917 /// handle.write_all(b"hello world")?;
923 #[stable(feature = "rust1", since = "1.0.0")]
924 pub fn stderr() -> Stderr {
925 // Note that unlike `stdout()` we don't use `at_exit` here to register a
926 // destructor. Stderr is not buffered , so there's no need to run a
927 // destructor for flushing the buffer
928 static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<StderrRaw>>> = SyncOnceCell::new();
931 inner: Pin::static_ref(&INSTANCE).get_or_init_pin(
932 || unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) },
933 |mutex| unsafe { mutex.init() },
938 /// Constructs a new locked handle to the standard error of the current
941 /// This handle is not buffered.
943 /// ### Note: Windows Portability Consideration
944 /// When operating in a console, the Windows implementation of this stream does not support
945 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
951 /// #![feature(stdio_locked)]
952 /// use std::io::{self, Write};
954 /// fn main() -> io::Result<()> {
955 /// let mut handle = io::stderr_locked();
957 /// handle.write_all(b"hello world")?;
962 #[unstable(feature = "stdio_locked", issue = "86845")]
963 pub fn stderr_locked() -> StderrLock<'static> {
964 stderr().into_locked()
968 /// Locks this handle to the standard error stream, returning a writable
971 /// The lock is released when the returned lock goes out of scope. The
972 /// returned guard also implements the [`Write`] trait for writing data.
977 /// use std::io::{self, Write};
979 /// fn foo() -> io::Result<()> {
980 /// let stderr = io::stderr();
981 /// let mut handle = stderr.lock();
983 /// handle.write_all(b"hello world")?;
988 #[stable(feature = "rust1", since = "1.0.0")]
989 pub fn lock(&self) -> StderrLock<'_> {
993 // Locks this handle with any lifetime. This depends on the
994 // implementation detail that the underlying `ReentrantMutex` is
996 fn lock_any<'a>(&self) -> StderrLock<'a> {
997 StderrLock { inner: self.inner.lock() }
1000 /// Locks and consumes this handle to the standard error stream,
1001 /// returning a writable guard.
1003 /// The lock is released when the returned guard goes out of scope. The
1004 /// returned guard also implements the [`Write`] trait for writing
1010 /// #![feature(stdio_locked)]
1011 /// use std::io::{self, Write};
1013 /// fn foo() -> io::Result<()> {
1014 /// let stderr = io::stderr();
1015 /// let mut handle = stderr.into_locked();
1017 /// handle.write_all(b"hello world")?;
1022 #[unstable(feature = "stdio_locked", issue = "86845")]
1023 pub fn into_locked(self) -> StderrLock<'static> {
1028 #[stable(feature = "std_debug", since = "1.16.0")]
1029 impl fmt::Debug for Stderr {
1030 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1031 f.debug_struct("Stderr").finish_non_exhaustive()
1035 #[stable(feature = "rust1", since = "1.0.0")]
1036 impl Write for Stderr {
1037 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1040 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
1041 (&*self).write_vectored(bufs)
1044 fn is_write_vectored(&self) -> bool {
1045 io::Write::is_write_vectored(&&*self)
1047 fn flush(&mut self) -> io::Result<()> {
1050 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
1051 (&*self).write_all(buf)
1053 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
1054 (&*self).write_all_vectored(bufs)
1056 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
1057 (&*self).write_fmt(args)
1061 #[stable(feature = "write_mt", since = "1.48.0")]
1062 impl Write for &Stderr {
1063 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1064 self.lock().write(buf)
1066 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
1067 self.lock().write_vectored(bufs)
1070 fn is_write_vectored(&self) -> bool {
1071 self.lock().is_write_vectored()
1073 fn flush(&mut self) -> io::Result<()> {
1076 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
1077 self.lock().write_all(buf)
1079 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
1080 self.lock().write_all_vectored(bufs)
1082 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
1083 self.lock().write_fmt(args)
1087 #[stable(feature = "rust1", since = "1.0.0")]
1088 impl Write for StderrLock<'_> {
1089 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1090 self.inner.borrow_mut().write(buf)
1092 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
1093 self.inner.borrow_mut().write_vectored(bufs)
1096 fn is_write_vectored(&self) -> bool {
1097 self.inner.borrow_mut().is_write_vectored()
1099 fn flush(&mut self) -> io::Result<()> {
1100 self.inner.borrow_mut().flush()
1102 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
1103 self.inner.borrow_mut().write_all(buf)
1105 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
1106 self.inner.borrow_mut().write_all_vectored(bufs)
1110 #[stable(feature = "std_debug", since = "1.16.0")]
1111 impl fmt::Debug for StderrLock<'_> {
1112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1113 f.debug_struct("StderrLock").finish_non_exhaustive()
1117 /// Sets the thread-local output capture buffer and returns the old one.
1119 feature = "internal_output_capture",
1120 reason = "this function is meant for use in the test crate \
1121 and may disappear in the future",
1125 pub fn set_output_capture(sink: Option<LocalStream>) -> Option<LocalStream> {
1126 if sink.is_none() && !OUTPUT_CAPTURE_USED.load(Ordering::Relaxed) {
1127 // OUTPUT_CAPTURE is definitely None since OUTPUT_CAPTURE_USED is false.
1130 OUTPUT_CAPTURE_USED.store(true, Ordering::Relaxed);
1131 OUTPUT_CAPTURE.with(move |slot| slot.replace(sink))
1134 /// Write `args` to the capture buffer if enabled and possible, or `global_s`
1135 /// otherwise. `label` identifies the stream in a panic message.
1137 /// This function is used to print error messages, so it takes extra
1138 /// care to avoid causing a panic when `local_s` is unusable.
1139 /// For instance, if the TLS key for the local stream is
1140 /// already destroyed, or if the local stream is locked by another
1141 /// thread, it will just fall back to the global stream.
1143 /// However, if the actual I/O causes an error, this function does panic.
1144 fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
1148 if OUTPUT_CAPTURE_USED.load(Ordering::Relaxed)
1149 && OUTPUT_CAPTURE.try_with(|s| {
1150 // Note that we completely remove a local sink to write to in case
1151 // our printing recursively panics/prints, so the recursive
1152 // panic/print goes to the global sink instead of our local sink.
1154 let _ = w.lock().unwrap_or_else(|e| e.into_inner()).write_fmt(args);
1159 // Successfully wrote to capture buffer.
1163 if let Err(e) = global_s().write_fmt(args) {
1164 panic!("failed printing to {}: {}", label, e);
1169 feature = "print_internals",
1170 reason = "implementation detail which may disappear or be replaced at any time",
1175 pub fn _print(args: fmt::Arguments<'_>) {
1176 print_to(args, stdout, "stdout");
1180 feature = "print_internals",
1181 reason = "implementation detail which may disappear or be replaced at any time",
1186 pub fn _eprint(args: fmt::Arguments<'_>) {
1187 print_to(args, stderr, "stderr");
1191 pub use realstd::io::{_eprint, _print};