1 #![cfg_attr(test, allow(unused))]
6 use crate::io::prelude::*;
8 use crate::cell::{Cell, RefCell};
10 use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
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()
112 unsafe fn initializer(&self) -> Initializer {
116 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
117 handle_ebadf(self.0.read_to_end(buf), 0)
120 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
121 handle_ebadf(self.0.read_to_string(buf), 0)
125 impl Write for StdoutRaw {
126 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
127 handle_ebadf(self.0.write(buf), buf.len())
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)
136 fn is_write_vectored(&self) -> bool {
137 self.0.is_write_vectored()
140 fn flush(&mut self) -> io::Result<()> {
141 handle_ebadf(self.0.flush(), ())
144 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
145 handle_ebadf(self.0.write_all(buf), ())
148 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
149 handle_ebadf(self.0.write_all_vectored(bufs), ())
152 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
153 handle_ebadf(self.0.write_fmt(fmt), ())
157 impl Write for StderrRaw {
158 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
159 handle_ebadf(self.0.write(buf), buf.len())
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)
168 fn is_write_vectored(&self) -> bool {
169 self.0.is_write_vectored()
172 fn flush(&mut self) -> io::Result<()> {
173 handle_ebadf(self.0.flush(), ())
176 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
177 handle_ebadf(self.0.write_all(buf), ())
180 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
181 handle_ebadf(self.0.write_all_vectored(bufs), ())
184 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
185 handle_ebadf(self.0.write_fmt(fmt), ())
189 fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
191 Err(ref e) if stdio::is_ebadf(e) => Ok(default),
196 /// A handle to the standard input stream of a process.
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
203 /// This handle implements the `Read` trait, but beware that concurrent reads
204 /// of `Stdin` must be executed with care.
206 /// Created by the [`io::stdin`] method.
208 /// [`io::stdin`]: stdin
210 /// ### Note: Windows Portability Consideration
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
219 /// use std::io::{self, Read};
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)?;
228 #[stable(feature = "rust1", since = "1.0.0")]
230 inner: &'static Mutex<BufReader<StdinRaw>>,
233 /// A locked reference to the [`Stdin`] handle.
235 /// This handle implements both the [`Read`] and [`BufRead`] traits, and
236 /// is constructed via the [`Stdin::lock`] method.
238 /// ### Note: Windows Portability Consideration
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
247 /// use std::io::{self, Read};
249 /// fn main() -> io::Result<()> {
250 /// let mut buffer = String::new();
251 /// let stdin = io::stdin(); // We get `Stdin` here.
253 /// let mut handle = stdin.lock(); // We get `StdinLock` here.
254 /// handle.read_to_string(&mut buffer)?;
255 /// } // `StdinLock` is dropped here.
259 #[stable(feature = "rust1", since = "1.0.0")]
260 pub struct StdinLock<'a> {
261 inner: MutexGuard<'a, BufReader<StdinRaw>>,
264 /// Constructs a new handle to the standard input of the current process.
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.
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
277 /// Using implicit synchronization:
280 /// use std::io::{self, Read};
282 /// fn main() -> io::Result<()> {
283 /// let mut buffer = String::new();
284 /// io::stdin().read_to_string(&mut buffer)?;
289 /// Using explicit synchronization:
292 /// use std::io::{self, Read};
294 /// fn main() -> io::Result<()> {
295 /// let mut buffer = String::new();
296 /// let stdin = io::stdin();
297 /// let mut handle = stdin.lock();
299 /// handle.read_to_string(&mut buffer)?;
303 #[stable(feature = "rust1", since = "1.0.0")]
304 pub fn stdin() -> Stdin {
305 static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
307 inner: INSTANCE.get_or_init(|| {
308 Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
314 /// Locks this handle to the standard input stream, returning a readable
317 /// The lock is released when the returned lock goes out of scope. The
318 /// returned guard also implements the [`Read`] and [`BufRead`] traits for
319 /// accessing the underlying data.
324 /// use std::io::{self, Read};
326 /// fn main() -> io::Result<()> {
327 /// let mut buffer = String::new();
328 /// let stdin = io::stdin();
329 /// let mut handle = stdin.lock();
331 /// handle.read_to_string(&mut buffer)?;
335 #[stable(feature = "rust1", since = "1.0.0")]
336 pub fn lock(&self) -> StdinLock<'_> {
337 StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
340 /// Locks this handle and reads a line of input, appending it to the specified buffer.
342 /// For detailed semantics of this method, see the documentation on
343 /// [`BufRead::read_line`].
350 /// let mut input = String::new();
351 /// match io::stdin().read_line(&mut input) {
353 /// println!("{} bytes read", n);
354 /// println!("{}", input);
356 /// Err(error) => println!("error: {}", error),
360 /// You can run the example one of two ways:
362 /// - Pipe some text to it, e.g., `printf foo | path/to/executable`
363 /// - Give it text interactively by running the executable directly,
364 /// in which case it will wait for the Enter key to be pressed before
366 #[stable(feature = "rust1", since = "1.0.0")]
367 pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
368 self.lock().read_line(buf)
372 #[stable(feature = "std_debug", since = "1.16.0")]
373 impl fmt::Debug for Stdin {
374 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
375 f.debug_struct("Stdin").finish_non_exhaustive()
379 #[stable(feature = "rust1", since = "1.0.0")]
380 impl Read for Stdin {
381 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
382 self.lock().read(buf)
384 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
385 self.lock().read_vectored(bufs)
388 fn is_read_vectored(&self) -> bool {
389 self.lock().is_read_vectored()
392 unsafe fn initializer(&self) -> Initializer {
395 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
396 self.lock().read_to_end(buf)
398 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
399 self.lock().read_to_string(buf)
401 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
402 self.lock().read_exact(buf)
406 // only used by platform-dependent io::copy specializations, i.e. unused on some platforms
407 #[cfg(any(target_os = "linux", target_os = "android"))]
409 pub(crate) fn as_mut_buf(&mut self) -> &mut BufReader<impl Read> {
414 #[stable(feature = "rust1", since = "1.0.0")]
415 impl Read for StdinLock<'_> {
416 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
420 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
421 self.inner.read_vectored(bufs)
425 fn is_read_vectored(&self) -> bool {
426 self.inner.is_read_vectored()
430 unsafe fn initializer(&self) -> Initializer {
434 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
435 self.inner.read_to_end(buf)
438 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
439 self.inner.read_to_string(buf)
442 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
443 self.inner.read_exact(buf)
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()
453 fn consume(&mut self, n: usize) {
454 self.inner.consume(n)
457 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
458 self.inner.read_until(byte, buf)
461 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
462 self.inner.read_line(buf)
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.debug_struct("StdinLock").finish_non_exhaustive()
473 /// A handle to the global standard output stream of the current process.
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.
479 /// Created by the [`io::stdout`] method.
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
486 /// [`lock`]: Stdout::lock
487 /// [`io::stdout`]: stdout
488 #[stable(feature = "rust1", since = "1.0.0")]
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: Pin<&'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
496 /// A locked reference to the [`Stdout`] handle.
498 /// This handle implements the [`Write`] trait, and is constructed via
499 /// the [`Stdout::lock`] method. See its documentation for more.
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
505 #[stable(feature = "rust1", since = "1.0.0")]
506 pub struct StdoutLock<'a> {
507 inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
510 static STDOUT: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = SyncOnceCell::new();
512 /// Constructs a new handle to the standard output of the current process.
514 /// Each handle returned is a reference to a shared global buffer whose access
515 /// is synchronized via a mutex. If you need more explicit control over
516 /// locking, see the [`Stdout::lock`] method.
518 /// ### Note: Windows Portability Consideration
519 /// When operating in a console, the Windows implementation of this stream does not support
520 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
525 /// Using implicit synchronization:
528 /// use std::io::{self, Write};
530 /// fn main() -> io::Result<()> {
531 /// io::stdout().write_all(b"hello world")?;
537 /// Using explicit synchronization:
540 /// use std::io::{self, Write};
542 /// fn main() -> io::Result<()> {
543 /// let stdout = io::stdout();
544 /// let mut handle = stdout.lock();
546 /// handle.write_all(b"hello world")?;
551 #[stable(feature = "rust1", since = "1.0.0")]
552 pub fn stdout() -> Stdout {
554 inner: Pin::static_ref(&STDOUT).get_or_init_pin(
555 || unsafe { ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw()))) },
556 |mutex| unsafe { mutex.init() },
562 if let Some(instance) = STDOUT.get() {
563 // Flush the data and disable buffering during shutdown
564 // by replacing the line writer by one with zero
565 // buffering capacity.
566 // We use try_lock() instead of lock(), because someone
567 // might have leaked a StdoutLock, which would
568 // otherwise cause a deadlock here.
569 if let Some(lock) = Pin::static_ref(instance).try_lock() {
570 *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
576 /// Locks this handle to the standard output stream, returning a writable
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.
585 /// use std::io::{self, Write};
587 /// fn main() -> io::Result<()> {
588 /// let stdout = io::stdout();
589 /// let mut handle = stdout.lock();
591 /// handle.write_all(b"hello world")?;
596 #[stable(feature = "rust1", since = "1.0.0")]
597 pub fn lock(&self) -> StdoutLock<'_> {
598 StdoutLock { inner: self.inner.lock() }
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.debug_struct("Stdout").finish_non_exhaustive()
609 #[stable(feature = "rust1", since = "1.0.0")]
610 impl Write for Stdout {
611 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
614 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
615 (&*self).write_vectored(bufs)
618 fn is_write_vectored(&self) -> bool {
619 io::Write::is_write_vectored(&&*self)
621 fn flush(&mut self) -> io::Result<()> {
624 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
625 (&*self).write_all(buf)
627 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
628 (&*self).write_all_vectored(bufs)
630 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
631 (&*self).write_fmt(args)
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)
640 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
641 self.lock().write_vectored(bufs)
644 fn is_write_vectored(&self) -> bool {
645 self.lock().is_write_vectored()
647 fn flush(&mut self) -> io::Result<()> {
650 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
651 self.lock().write_all(buf)
653 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
654 self.lock().write_all_vectored(bufs)
656 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
657 self.lock().write_fmt(args)
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)
666 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
667 self.inner.borrow_mut().write_vectored(bufs)
670 fn is_write_vectored(&self) -> bool {
671 self.inner.borrow_mut().is_write_vectored()
673 fn flush(&mut self) -> io::Result<()> {
674 self.inner.borrow_mut().flush()
676 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
677 self.inner.borrow_mut().write_all(buf)
679 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
680 self.inner.borrow_mut().write_all_vectored(bufs)
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.debug_struct("StdoutLock").finish_non_exhaustive()
691 /// A handle to the standard error stream of a process.
693 /// For more information, see the [`io::stderr`] method.
695 /// [`io::stderr`]: stderr
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
701 #[stable(feature = "rust1", since = "1.0.0")]
703 inner: Pin<&'static ReentrantMutex<RefCell<StderrRaw>>>,
706 /// A locked reference to the [`Stderr`] handle.
708 /// This handle implements the [`Write`] trait and is constructed via
709 /// the [`Stderr::lock`] method. See its documentation for more.
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
715 #[stable(feature = "rust1", since = "1.0.0")]
716 pub struct StderrLock<'a> {
717 inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
720 /// Constructs a new handle to the standard error of the current process.
722 /// This handle is not buffered.
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
731 /// Using implicit synchronization:
734 /// use std::io::{self, Write};
736 /// fn main() -> io::Result<()> {
737 /// io::stderr().write_all(b"hello world")?;
743 /// Using explicit synchronization:
746 /// use std::io::{self, Write};
748 /// fn main() -> io::Result<()> {
749 /// let stderr = io::stderr();
750 /// let mut handle = stderr.lock();
752 /// handle.write_all(b"hello world")?;
757 #[stable(feature = "rust1", since = "1.0.0")]
758 pub fn stderr() -> Stderr {
759 // Note that unlike `stdout()` we don't use `at_exit` here to register a
760 // destructor. Stderr is not buffered , so there's no need to run a
761 // destructor for flushing the buffer
762 static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<StderrRaw>>> = SyncOnceCell::new();
765 inner: Pin::static_ref(&INSTANCE).get_or_init_pin(
766 || unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) },
767 |mutex| unsafe { mutex.init() },
773 /// Locks this handle to the standard error stream, returning a writable
776 /// The lock is released when the returned lock goes out of scope. The
777 /// returned guard also implements the [`Write`] trait for writing data.
782 /// use std::io::{self, Write};
784 /// fn foo() -> io::Result<()> {
785 /// let stderr = io::stderr();
786 /// let mut handle = stderr.lock();
788 /// handle.write_all(b"hello world")?;
793 #[stable(feature = "rust1", since = "1.0.0")]
794 pub fn lock(&self) -> StderrLock<'_> {
795 StderrLock { inner: self.inner.lock() }
799 #[stable(feature = "std_debug", since = "1.16.0")]
800 impl fmt::Debug for Stderr {
801 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
802 f.debug_struct("Stderr").finish_non_exhaustive()
806 #[stable(feature = "rust1", since = "1.0.0")]
807 impl Write for Stderr {
808 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
811 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
812 (&*self).write_vectored(bufs)
815 fn is_write_vectored(&self) -> bool {
816 io::Write::is_write_vectored(&&*self)
818 fn flush(&mut self) -> io::Result<()> {
821 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
822 (&*self).write_all(buf)
824 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
825 (&*self).write_all_vectored(bufs)
827 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
828 (&*self).write_fmt(args)
832 #[stable(feature = "write_mt", since = "1.48.0")]
833 impl Write for &Stderr {
834 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
835 self.lock().write(buf)
837 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
838 self.lock().write_vectored(bufs)
841 fn is_write_vectored(&self) -> bool {
842 self.lock().is_write_vectored()
844 fn flush(&mut self) -> io::Result<()> {
847 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
848 self.lock().write_all(buf)
850 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
851 self.lock().write_all_vectored(bufs)
853 fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
854 self.lock().write_fmt(args)
858 #[stable(feature = "rust1", since = "1.0.0")]
859 impl Write for StderrLock<'_> {
860 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
861 self.inner.borrow_mut().write(buf)
863 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
864 self.inner.borrow_mut().write_vectored(bufs)
867 fn is_write_vectored(&self) -> bool {
868 self.inner.borrow_mut().is_write_vectored()
870 fn flush(&mut self) -> io::Result<()> {
871 self.inner.borrow_mut().flush()
873 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
874 self.inner.borrow_mut().write_all(buf)
876 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
877 self.inner.borrow_mut().write_all_vectored(bufs)
881 #[stable(feature = "std_debug", since = "1.16.0")]
882 impl fmt::Debug for StderrLock<'_> {
883 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
884 f.debug_struct("StderrLock").finish_non_exhaustive()
888 /// Sets the thread-local output capture buffer and returns the old one.
890 feature = "internal_output_capture",
891 reason = "this function is meant for use in the test crate \
892 and may disappear in the future",
896 pub fn set_output_capture(sink: Option<LocalStream>) -> Option<LocalStream> {
897 if sink.is_none() && !OUTPUT_CAPTURE_USED.load(Ordering::Relaxed) {
898 // OUTPUT_CAPTURE is definitely None since OUTPUT_CAPTURE_USED is false.
901 OUTPUT_CAPTURE_USED.store(true, Ordering::Relaxed);
902 OUTPUT_CAPTURE.with(move |slot| slot.replace(sink))
905 /// Write `args` to the capture buffer if enabled and possible, or `global_s`
906 /// otherwise. `label` identifies the stream in a panic message.
908 /// This function is used to print error messages, so it takes extra
909 /// care to avoid causing a panic when `local_s` is unusable.
910 /// For instance, if the TLS key for the local stream is
911 /// already destroyed, or if the local stream is locked by another
912 /// thread, it will just fall back to the global stream.
914 /// However, if the actual I/O causes an error, this function does panic.
915 fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
919 if OUTPUT_CAPTURE_USED.load(Ordering::Relaxed)
920 && OUTPUT_CAPTURE.try_with(|s| {
921 // Note that we completely remove a local sink to write to in case
922 // our printing recursively panics/prints, so the recursive
923 // panic/print goes to the global sink instead of our local sink.
925 let _ = w.lock().unwrap_or_else(|e| e.into_inner()).write_fmt(args);
930 // Succesfully wrote to capture buffer.
934 if let Err(e) = global_s().write_fmt(args) {
935 panic!("failed printing to {}: {}", label, e);
940 feature = "print_internals",
941 reason = "implementation detail which may disappear or be replaced at any time",
946 pub fn _print(args: fmt::Arguments<'_>) {
947 print_to(args, stdout, "stdout");
951 feature = "print_internals",
952 reason = "implementation detail which may disappear or be replaced at any time",
957 pub fn _eprint(args: fmt::Arguments<'_>) {
958 print_to(args, stderr, "stderr");
962 pub use realstd::io::{_eprint, _print};