2019-02-14 12:36:01 +00:00
|
|
|
#![cfg_attr(test, allow(unused))]
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::io::prelude::*;
|
2015-02-25 07:27:20 +00:00
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::cell::RefCell;
|
|
|
|
use crate::fmt;
|
|
|
|
use crate::io::lazy::Lazy;
|
2019-12-22 22:42:04 +00:00
|
|
|
use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
|
2020-03-12 18:39:30 +00:00
|
|
|
use crate::sync::{Arc, Mutex, MutexGuard, Once};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys::stdio;
|
|
|
|
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
|
|
|
use crate::thread::LocalKey;
|
2015-02-25 07:27:20 +00:00
|
|
|
|
2015-03-08 22:30:15 +00:00
|
|
|
thread_local! {
|
2019-01-24 20:49:03 +00:00
|
|
|
/// Stdout used by print! and println! macros
|
2018-07-10 18:35:36 +00:00
|
|
|
static LOCAL_STDOUT: RefCell<Option<Box<dyn Write + Send>>> = {
|
2015-03-08 22:30:15 +00:00
|
|
|
RefCell::new(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:36:01 +00:00
|
|
|
thread_local! {
|
2019-01-24 20:49:03 +00:00
|
|
|
/// Stderr used by eprint! and eprintln! macros, and panics
|
2019-02-14 12:36:01 +00:00
|
|
|
static LOCAL_STDERR: RefCell<Option<Box<dyn Write + Send>>> = {
|
|
|
|
RefCell::new(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 07:27:20 +00:00
|
|
|
/// A handle to a raw instance of the standard input stream of this process.
|
|
|
|
///
|
|
|
|
/// This handle is not synchronized or buffered in any fashion. Constructed via
|
2015-03-15 01:08:09 +00:00
|
|
|
/// the `std::io::stdio::stdin_raw` function.
|
|
|
|
struct StdinRaw(stdio::Stdin);
|
2015-02-25 07:27:20 +00:00
|
|
|
|
|
|
|
/// A handle to a raw instance of the standard output stream of this process.
|
|
|
|
///
|
|
|
|
/// This handle is not synchronized or buffered in any fashion. Constructed via
|
2015-03-15 01:08:09 +00:00
|
|
|
/// the `std::io::stdio::stdout_raw` function.
|
|
|
|
struct StdoutRaw(stdio::Stdout);
|
2015-02-25 07:27:20 +00:00
|
|
|
|
|
|
|
/// A handle to a raw instance of the standard output stream of this process.
|
|
|
|
///
|
|
|
|
/// This handle is not synchronized or buffered in any fashion. Constructed via
|
2015-03-15 01:08:09 +00:00
|
|
|
/// the `std::io::stdio::stderr_raw` function.
|
|
|
|
struct StderrRaw(stdio::Stderr);
|
2015-02-25 07:27:20 +00:00
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Constructs a new raw handle to the standard input of this process.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
|
|
|
/// The returned handle does not interact with any other handles created nor
|
|
|
|
/// handles returned by `std::io::stdin`. Data buffered by the `std::io::stdin`
|
|
|
|
/// handles is **not** available to raw handles returned from this function.
|
|
|
|
///
|
|
|
|
/// The returned handle has no external synchronization or buffering.
|
2020-08-20 00:00:00 +00:00
|
|
|
#[unstable(feature = "libstd_sys_internals", issue = "none")]
|
|
|
|
const fn stdin_raw() -> StdinRaw {
|
2020-08-20 00:00:00 +00:00
|
|
|
StdinRaw(stdio::Stdin::new())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
|
2015-08-12 00:58:15 +00:00
|
|
|
/// Constructs a new raw handle to the standard output stream of this process.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
|
|
|
/// The returned handle does not interact with any other handles created nor
|
|
|
|
/// handles returned by `std::io::stdout`. Note that data is buffered by the
|
2015-08-12 00:58:15 +00:00
|
|
|
/// `std::io::stdout` handles so writes which happen via this raw handle may
|
2015-02-25 07:27:20 +00:00
|
|
|
/// appear before previous writes.
|
|
|
|
///
|
|
|
|
/// The returned handle has no external synchronization or buffering layered on
|
|
|
|
/// top.
|
2020-08-20 00:00:00 +00:00
|
|
|
#[unstable(feature = "libstd_sys_internals", issue = "none")]
|
|
|
|
const fn stdout_raw() -> StdoutRaw {
|
2020-08-20 00:00:00 +00:00
|
|
|
StdoutRaw(stdio::Stdout::new())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
|
2015-08-12 00:58:15 +00:00
|
|
|
/// Constructs a new raw handle to the standard error stream of this process.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
|
|
|
/// The returned handle does not interact with any other handles created nor
|
2015-08-12 00:58:15 +00:00
|
|
|
/// handles returned by `std::io::stderr`.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
|
|
|
/// The returned handle has no external synchronization or buffering layered on
|
|
|
|
/// top.
|
2020-08-20 00:00:00 +00:00
|
|
|
#[unstable(feature = "libstd_sys_internals", issue = "none")]
|
|
|
|
const fn stderr_raw() -> StderrRaw {
|
2020-08-20 00:00:00 +00:00
|
|
|
StderrRaw(stdio::Stderr::new())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
|
|
|
|
impl Read for StdinRaw {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.read(buf), 0)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.read_vectored(bufs), 0)
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
self.0.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
2016-02-12 08:17:24 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.read_to_end(buf), 0)
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.read_to_string(buf), 0)
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
2015-02-25 07:27:20 +00:00
|
|
|
impl Write for StdoutRaw {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write(buf), buf.len())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
let total = bufs.iter().map(|b| b.len()).sum();
|
|
|
|
handle_ebadf(self.0.write_vectored(bufs), total)
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.0.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.flush(), ())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write_all(buf), ())
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write_all_vectored(bufs), ())
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write_fmt(fmt), ())
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
2015-02-25 07:27:20 +00:00
|
|
|
impl Write for StderrRaw {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write(buf), buf.len())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2020-08-21 00:00:00 +00:00
|
|
|
let total = bufs.iter().map(|b| b.len()).sum();
|
|
|
|
handle_ebadf(self.0.write_vectored(bufs), total)
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.0.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.flush(), ())
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write_all(buf), ())
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write_all_vectored(bufs), ())
|
2020-05-28 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
|
2020-08-21 00:00:00 +00:00
|
|
|
handle_ebadf(self.0.write_fmt(fmt), ())
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
2015-06-10 04:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
|
|
|
|
match r {
|
2017-11-01 19:50:13 +00:00
|
|
|
Err(ref e) if stdio::is_ebadf(e) => Ok(default),
|
2019-12-22 22:42:04 +00:00
|
|
|
r => r,
|
2015-06-10 04:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 07:27:20 +00:00
|
|
|
/// A handle to the standard input stream of a process.
|
|
|
|
///
|
|
|
|
/// Each handle is a shared reference to a global buffer of input data to this
|
2015-12-30 10:41:04 +00:00
|
|
|
/// process. A handle can be `lock`'d to gain full access to [`BufRead`] methods
|
2018-11-27 02:59:49 +00:00
|
|
|
/// (e.g., `.lines()`). Reads to this handle are otherwise locked with respect
|
2016-03-23 23:39:01 +00:00
|
|
|
/// to other reads.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
|
|
|
/// This handle implements the `Read` trait, but beware that concurrent reads
|
|
|
|
/// of `Stdin` must be executed with care.
|
2015-04-22 20:57:08 +00:00
|
|
|
///
|
2015-12-30 10:41:04 +00:00
|
|
|
/// Created by the [`io::stdin`] method.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
/// [`io::stdin`]: stdin
|
2019-02-04 06:38:43 +00:00
|
|
|
///
|
|
|
|
/// ### Note: Windows Portability Consideration
|
2020-06-26 11:31:36 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
2020-06-26 11:31:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// let mut stdin = io::stdin(); // We get `Stdin` here.
|
|
|
|
/// stdin.read_to_string(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub struct Stdin {
|
2020-08-21 00:00:00 +00:00
|
|
|
inner: Arc<Mutex<BufReader<StdinRaw>>>,
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:25 +00:00
|
|
|
/// A locked reference to the `Stdin` handle.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2015-12-30 10:41:04 +00:00
|
|
|
/// This handle implements both the [`Read`] and [`BufRead`] traits, and
|
|
|
|
/// is constructed via the [`Stdin::lock`] method.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
2020-06-26 11:31:36 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
2020-06-26 11:31:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// let stdin = io::stdin(); // We get `Stdin` here.
|
|
|
|
/// {
|
|
|
|
/// let mut stdin_lock = stdin.lock(); // We get `StdinLock` here.
|
|
|
|
/// stdin_lock.read_to_string(&mut buffer)?;
|
|
|
|
/// } // `StdinLock` is dropped here.
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub struct StdinLock<'a> {
|
2020-08-21 00:00:00 +00:00
|
|
|
inner: MutexGuard<'a, BufReader<StdinRaw>>,
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:31:08 +00:00
|
|
|
/// Constructs a new handle to the standard input of the current process.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2015-07-08 22:31:08 +00:00
|
|
|
/// Each handle returned is a reference to a shared global buffer whose access
|
|
|
|
/// is synchronized via a mutex. If you need more explicit control over
|
2019-09-11 12:03:40 +00:00
|
|
|
/// locking, see the [`Stdin::lock`] method.
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
|
|
|
///
|
2015-07-08 22:31:08 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using implicit synchronization:
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-07-08 22:31:08 +00:00
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// io::stdin().read_to_string(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using explicit synchronization:
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-07-08 22:31:08 +00:00
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut handle = stdin.lock();
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// handle.read_to_string(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub fn stdin() -> Stdin {
|
2020-08-21 00:00:00 +00:00
|
|
|
static INSTANCE: Lazy<Mutex<BufReader<StdinRaw>>> = Lazy::new();
|
2015-02-25 07:27:20 +00:00
|
|
|
return Stdin {
|
2019-12-22 22:42:04 +00:00
|
|
|
inner: unsafe { INSTANCE.get(stdin_init).expect("cannot access stdin during shutdown") },
|
2015-02-25 07:27:20 +00:00
|
|
|
};
|
|
|
|
|
2020-08-21 00:00:00 +00:00
|
|
|
fn stdin_init() -> Arc<Mutex<BufReader<StdinRaw>>> {
|
2018-08-06 11:52:15 +00:00
|
|
|
// This must not reentrantly access `INSTANCE`
|
2020-08-21 00:00:00 +00:00
|
|
|
let stdin = stdin_raw();
|
2016-09-30 21:01:53 +00:00
|
|
|
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stdin {
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Locks this handle to the standard input stream, returning a readable
|
2015-02-25 07:27:20 +00:00
|
|
|
/// guard.
|
|
|
|
///
|
|
|
|
/// The lock is released when the returned lock goes out of scope. The
|
2015-12-30 10:41:04 +00:00
|
|
|
/// returned guard also implements the [`Read`] and [`BufRead`] traits for
|
2015-02-25 07:27:20 +00:00
|
|
|
/// accessing the underlying data.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2016-07-29 22:56:14 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2016-07-29 22:56:14 +00:00
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut handle = stdin.lock();
|
2016-07-29 22:56:14 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// handle.read_to_string(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2016-07-29 22:56:14 +00:00
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-01 08:34:11 +00:00
|
|
|
pub fn lock(&self) -> StdinLock<'_> {
|
2015-03-18 06:05:44 +00:00
|
|
|
StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
|
2020-02-23 20:19:25 +00:00
|
|
|
/// Locks this handle and reads a line of input, appending it to the specified buffer.
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
///
|
|
|
|
/// For detailed semantics of this method, see the documentation on
|
2015-12-30 10:41:04 +00:00
|
|
|
/// [`BufRead::read_line`].
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2015-06-27 20:58:49 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
|
|
|
///
|
|
|
|
/// let mut input = String::new();
|
|
|
|
/// match io::stdin().read_line(&mut input) {
|
|
|
|
/// Ok(n) => {
|
|
|
|
/// println!("{} bytes read", n);
|
|
|
|
/// println!("{}", input);
|
|
|
|
/// }
|
|
|
|
/// Err(error) => println!("error: {}", error),
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You can run the example one of two ways:
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// - Pipe some text to it, e.g., `printf foo | path/to/executable`
|
2015-06-27 20:58:49 +00:00
|
|
|
/// - Give it text interactively by running the executable directly,
|
2015-12-23 15:46:59 +00:00
|
|
|
/// in which case it will wait for the Enter key to be pressed before
|
2015-06-27 20:58:49 +00:00
|
|
|
/// continuing
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-07-08 19:53:45 +00:00
|
|
|
pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
self.lock().read_line(buf)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
impl fmt::Debug for Stdin {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
f.pad("Stdin { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
impl Read for Stdin {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.lock().read(buf)
|
|
|
|
}
|
2019-04-27 15:34:08 +00:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.lock().read_vectored(bufs)
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
self.lock().is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
2017-05-15 01:29:18 +00:00
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
|
|
|
}
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
2015-02-25 07:27:20 +00:00
|
|
|
self.lock().read_to_end(buf)
|
|
|
|
}
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
|
2015-02-25 07:27:20 +00:00
|
|
|
self.lock().read_to_string(buf)
|
|
|
|
}
|
2015-07-20 03:23:37 +00:00
|
|
|
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
|
|
|
|
self.lock().read_exact(buf)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Read for StdinLock<'_> {
|
2015-02-25 07:27:20 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.inner.read_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
self.inner.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
2015-07-10 16:34:07 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
self.inner.read_to_end(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
|
|
|
|
self.inner.read_to_string(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
|
|
|
|
self.inner.read_exact(buf)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
2015-06-10 04:39:36 +00:00
|
|
|
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl BufRead for StdinLock<'_> {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
|
|
self.inner.fill_buf()
|
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn consume(&mut self, n: usize) {
|
|
|
|
self.inner.consume(n)
|
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
|
|
|
|
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
self.inner.read_until(byte, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
|
|
|
|
self.inner.read_line(buf)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl fmt::Debug for StdinLock<'_> {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
f.pad("StdinLock { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 07:27:20 +00:00
|
|
|
/// A handle to the global standard output stream of the current process.
|
|
|
|
///
|
|
|
|
/// Each handle shares a global buffer of data to be written to the standard
|
|
|
|
/// output stream. Access is also synchronized via a lock and explicit control
|
2017-03-12 18:04:52 +00:00
|
|
|
/// over locking is available via the [`lock`] method.
|
2015-04-22 20:57:08 +00:00
|
|
|
///
|
2015-12-30 10:41:04 +00:00
|
|
|
/// Created by the [`io::stdout`] method.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
/// [`lock`]: Stdout::lock
|
|
|
|
/// [`io::stdout`]: stdout
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub struct Stdout {
|
|
|
|
// FIXME: this should be LineWriter or BufWriter depending on the state of
|
|
|
|
// stdout (tty or not). Note that if this is not line buffered it
|
|
|
|
// should also flush-on-panic or some form of flush-on-abort.
|
2020-08-21 00:00:00 +00:00
|
|
|
inner: Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:25 +00:00
|
|
|
/// A locked reference to the `Stdout` handle.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2015-12-30 10:41:04 +00:00
|
|
|
/// This handle implements the [`Write`] trait, and is constructed via
|
|
|
|
/// the [`Stdout::lock`] method.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub struct StdoutLock<'a> {
|
2020-08-21 00:00:00 +00:00
|
|
|
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:31:08 +00:00
|
|
|
/// Constructs a new handle to the standard output of the current process.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
|
|
|
/// Each handle returned is a reference to a shared global buffer whose access
|
2015-07-08 22:31:08 +00:00
|
|
|
/// is synchronized via a mutex. If you need more explicit control over
|
2019-09-11 12:03:40 +00:00
|
|
|
/// locking, see the [`Stdout::lock`] method.
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
|
|
|
///
|
2015-07-08 22:31:08 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using implicit synchronization:
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-07-08 22:31:08 +00:00
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
2019-03-28 18:28:39 +00:00
|
|
|
/// io::stdout().write_all(b"hello world")?;
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using explicit synchronization:
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-07-08 22:31:08 +00:00
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let stdout = io::stdout();
|
|
|
|
/// let mut handle = stdout.lock();
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
2019-03-28 18:28:39 +00:00
|
|
|
/// handle.write_all(b"hello world")?;
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub fn stdout() -> Stdout {
|
2020-08-21 00:00:00 +00:00
|
|
|
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = Lazy::new();
|
2015-02-25 07:27:20 +00:00
|
|
|
return Stdout {
|
2019-12-22 22:42:04 +00:00
|
|
|
inner: unsafe { INSTANCE.get(stdout_init).expect("cannot access stdout during shutdown") },
|
2015-02-25 07:27:20 +00:00
|
|
|
};
|
|
|
|
|
2020-08-21 00:00:00 +00:00
|
|
|
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> {
|
2018-08-06 11:52:15 +00:00
|
|
|
// This must not reentrantly access `INSTANCE`
|
2020-08-21 00:00:00 +00:00
|
|
|
let stdout = stdout_raw();
|
2020-03-12 18:39:30 +00:00
|
|
|
unsafe {
|
|
|
|
let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout))));
|
|
|
|
ret.init();
|
2020-03-29 18:19:14 +00:00
|
|
|
ret
|
2020-03-12 18:39:30 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stdout {
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Locks this handle to the standard output stream, returning a writable
|
2015-02-25 07:27:20 +00:00
|
|
|
/// guard.
|
|
|
|
///
|
|
|
|
/// The lock is released when the returned lock goes out of scope. The
|
|
|
|
/// returned guard also implements the `Write` trait for writing data.
|
2016-07-29 22:57:20 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2016-07-29 22:57:20 +00:00
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let stdout = io::stdout();
|
|
|
|
/// let mut handle = stdout.lock();
|
2016-07-29 22:57:20 +00:00
|
|
|
///
|
2019-03-28 18:28:39 +00:00
|
|
|
/// handle.write_all(b"hello world")?;
|
2016-07-29 22:57:20 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2016-07-29 22:57:20 +00:00
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-01 08:34:11 +00:00
|
|
|
pub fn lock(&self) -> StdoutLock<'_> {
|
2020-03-12 18:39:30 +00:00
|
|
|
StdoutLock { inner: self.inner.lock() }
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
impl fmt::Debug for Stdout {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
f.pad("Stdout { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
impl Write for Stdout {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.lock().write(buf)
|
|
|
|
}
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.lock().write_vectored(bufs)
|
|
|
|
}
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.lock().is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.lock().flush()
|
|
|
|
}
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
|
|
|
self.lock().write_all(buf)
|
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
|
|
|
|
self.lock().write_all_vectored(bufs)
|
|
|
|
}
|
2020-06-17 23:48:51 +00:00
|
|
|
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
|
|
|
|
self.lock().write_fmt(args)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Write for StdoutLock<'_> {
|
2015-02-25 07:27:20 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2016-03-10 19:09:02 +00:00
|
|
|
self.inner.borrow_mut().write(buf)
|
2015-04-03 21:46:54 +00:00
|
|
|
}
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.inner.borrow_mut().write_vectored(bufs)
|
|
|
|
}
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.inner.borrow_mut().is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
2015-04-03 21:46:54 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().flush()
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().write_all(buf)
|
|
|
|
}
|
|
|
|
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().write_all_vectored(bufs)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl fmt::Debug for StdoutLock<'_> {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
f.pad("StdoutLock { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 07:27:20 +00:00
|
|
|
/// A handle to the standard error stream of a process.
|
|
|
|
///
|
2015-12-30 10:41:04 +00:00
|
|
|
/// For more information, see the [`io::stderr`] method.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
/// [`io::stderr`]: stderr
|
2019-02-04 06:38:43 +00:00
|
|
|
///
|
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub struct Stderr {
|
2020-08-21 00:00:00 +00:00
|
|
|
inner: &'static ReentrantMutex<RefCell<StderrRaw>>,
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:11:25 +00:00
|
|
|
/// A locked reference to the `Stderr` handle.
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2015-12-24 00:52:27 +00:00
|
|
|
/// This handle implements the `Write` trait and is constructed via
|
2015-12-30 10:41:04 +00:00
|
|
|
/// the [`Stderr::lock`] method.
|
2015-12-24 00:52:27 +00:00
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub struct StderrLock<'a> {
|
2020-08-21 00:00:00 +00:00
|
|
|
inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:31:08 +00:00
|
|
|
/// Constructs a new handle to the standard error of the current process.
|
|
|
|
///
|
|
|
|
/// This handle is not buffered.
|
|
|
|
///
|
2019-02-04 06:38:43 +00:00
|
|
|
/// ### Note: Windows Portability Consideration
|
|
|
|
/// When operating in a console, the Windows implementation of this stream does not support
|
|
|
|
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
|
|
|
|
/// an error.
|
|
|
|
///
|
2015-07-08 22:31:08 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using implicit synchronization:
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-07-08 22:31:08 +00:00
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
2019-03-28 18:28:39 +00:00
|
|
|
/// io::stderr().write_all(b"hello world")?;
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using explicit synchronization:
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-07-08 22:31:08 +00:00
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let stderr = io::stderr();
|
|
|
|
/// let mut handle = stderr.lock();
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2019-03-28 18:28:39 +00:00
|
|
|
/// handle.write_all(b"hello world")?;
|
2015-02-25 07:27:20 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
pub fn stderr() -> Stderr {
|
2020-03-12 18:39:30 +00:00
|
|
|
// Note that unlike `stdout()` we don't use `Lazy` here which registers a
|
|
|
|
// destructor. Stderr is not buffered nor does the `stderr_raw` type consume
|
|
|
|
// any owned resources, so there's no need to run any destructors at some
|
|
|
|
// point in the future.
|
|
|
|
//
|
|
|
|
// This has the added benefit of allowing `stderr` to be usable during
|
|
|
|
// process shutdown as well!
|
2020-08-21 00:00:00 +00:00
|
|
|
static INSTANCE: ReentrantMutex<RefCell<StderrRaw>> =
|
|
|
|
unsafe { ReentrantMutex::new(RefCell::new(stderr_raw())) };
|
2020-03-12 18:39:30 +00:00
|
|
|
|
|
|
|
// When accessing stderr we need one-time initialization of the reentrant
|
2020-08-21 00:00:00 +00:00
|
|
|
// mutex. Afterwards we can just always use the now-filled-in `INSTANCE` value.
|
2020-03-12 18:39:30 +00:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| unsafe {
|
|
|
|
INSTANCE.init();
|
|
|
|
});
|
2020-03-29 18:19:14 +00:00
|
|
|
Stderr { inner: &INSTANCE }
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Stderr {
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Locks this handle to the standard error stream, returning a writable
|
2015-02-25 07:27:20 +00:00
|
|
|
/// guard.
|
|
|
|
///
|
|
|
|
/// The lock is released when the returned lock goes out of scope. The
|
2020-08-18 17:36:52 +00:00
|
|
|
/// returned guard also implements the [`Write`] trait for writing data.
|
2016-07-29 22:53:18 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// fn foo() -> io::Result<()> {
|
|
|
|
/// let stderr = io::stderr();
|
|
|
|
/// let mut handle = stderr.lock();
|
|
|
|
///
|
2019-03-28 18:28:39 +00:00
|
|
|
/// handle.write_all(b"hello world")?;
|
2016-07-29 22:53:18 +00:00
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-01 08:34:11 +00:00
|
|
|
pub fn lock(&self) -> StderrLock<'_> {
|
2020-03-12 18:39:30 +00:00
|
|
|
StderrLock { inner: self.inner.lock() }
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
impl fmt::Debug for Stderr {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
f.pad("Stderr { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-25 07:27:20 +00:00
|
|
|
impl Write for Stderr {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.lock().write(buf)
|
|
|
|
}
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.lock().write_vectored(bufs)
|
|
|
|
}
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.lock().is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.lock().flush()
|
|
|
|
}
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
|
|
|
self.lock().write_all(buf)
|
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
|
|
|
|
self.lock().write_all_vectored(bufs)
|
|
|
|
}
|
2020-06-17 23:48:51 +00:00
|
|
|
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
|
|
|
|
self.lock().write_fmt(args)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 21:16:46 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Write for StderrLock<'_> {
|
2015-02-25 07:27:20 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2016-03-10 19:09:02 +00:00
|
|
|
self.inner.borrow_mut().write(buf)
|
2015-04-03 21:46:54 +00:00
|
|
|
}
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
self.inner.borrow_mut().write_vectored(bufs)
|
|
|
|
}
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.inner.borrow_mut().is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
2015-04-03 21:46:54 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().flush()
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
2020-05-28 19:02:48 +00:00
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().write_all(buf)
|
|
|
|
}
|
|
|
|
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().write_all_vectored(bufs)
|
|
|
|
}
|
2015-02-25 07:27:20 +00:00
|
|
|
}
|
2015-03-11 22:24:14 +00:00
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl fmt::Debug for StderrLock<'_> {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
f.pad("StderrLock { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:12:29 +00:00
|
|
|
/// Resets the thread-local stderr handle to the specified writer
|
2015-03-11 22:24:14 +00:00
|
|
|
///
|
2015-05-08 15:12:29 +00:00
|
|
|
/// This will replace the current thread's stderr handle, returning the old
|
2015-03-08 22:30:15 +00:00
|
|
|
/// handle. All future calls to `panic!` and friends will emit their output to
|
2015-03-11 22:24:14 +00:00
|
|
|
/// this specified handle.
|
|
|
|
///
|
2015-05-08 15:12:29 +00:00
|
|
|
/// Note that this does not need to be called for all new threads; the default
|
2015-03-08 22:30:15 +00:00
|
|
|
/// output handle is to the process's stderr stream.
|
2019-12-22 22:42:04 +00:00
|
|
|
#[unstable(
|
|
|
|
feature = "set_stdio",
|
|
|
|
reason = "this function may disappear completely or be replaced \
|
2015-08-13 17:12:38 +00:00
|
|
|
with a more general mechanism",
|
2019-12-22 22:42:04 +00:00
|
|
|
issue = "none"
|
|
|
|
)]
|
2015-03-11 22:24:14 +00:00
|
|
|
#[doc(hidden)]
|
2018-07-10 18:35:36 +00:00
|
|
|
pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::mem;
|
2019-12-22 22:42:04 +00:00
|
|
|
LOCAL_STDERR.with(move |slot| mem::replace(&mut *slot.borrow_mut(), sink)).and_then(|mut s| {
|
2015-03-11 22:24:14 +00:00
|
|
|
let _ = s.flush();
|
|
|
|
Some(s)
|
|
|
|
})
|
|
|
|
}
|
2015-03-08 22:30:15 +00:00
|
|
|
|
2015-05-08 15:12:29 +00:00
|
|
|
/// Resets the thread-local stdout handle to the specified writer
|
2015-03-08 22:30:15 +00:00
|
|
|
///
|
2015-05-08 15:12:29 +00:00
|
|
|
/// This will replace the current thread's stdout handle, returning the old
|
2015-03-08 22:30:15 +00:00
|
|
|
/// handle. All future calls to `print!` and friends will emit their output to
|
|
|
|
/// this specified handle.
|
|
|
|
///
|
2015-05-08 15:12:29 +00:00
|
|
|
/// Note that this does not need to be called for all new threads; the default
|
2015-03-08 22:30:15 +00:00
|
|
|
/// output handle is to the process's stdout stream.
|
2019-12-22 22:42:04 +00:00
|
|
|
#[unstable(
|
|
|
|
feature = "set_stdio",
|
|
|
|
reason = "this function may disappear completely or be replaced \
|
2015-08-13 17:12:38 +00:00
|
|
|
with a more general mechanism",
|
2019-12-22 22:42:04 +00:00
|
|
|
issue = "none"
|
|
|
|
)]
|
2015-03-08 22:30:15 +00:00
|
|
|
#[doc(hidden)]
|
2018-07-10 18:35:36 +00:00
|
|
|
pub fn set_print(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::mem;
|
2019-12-22 22:42:04 +00:00
|
|
|
LOCAL_STDOUT.with(move |slot| mem::replace(&mut *slot.borrow_mut(), sink)).and_then(|mut s| {
|
2015-03-08 22:30:15 +00:00
|
|
|
let _ = s.flush();
|
|
|
|
Some(s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:48:09 +00:00
|
|
|
/// Write `args` to output stream `local_s` if possible, `global_s`
|
|
|
|
/// otherwise. `label` identifies the stream in a panic message.
|
|
|
|
///
|
|
|
|
/// This function is used to print error messages, so it takes extra
|
2019-10-20 21:13:41 +00:00
|
|
|
/// care to avoid causing a panic when `local_s` is unusable.
|
2018-02-28 23:07:27 +00:00
|
|
|
/// For instance, if the TLS key for the local stream is
|
|
|
|
/// already destroyed, or if the local stream is locked by another
|
2017-04-13 14:48:09 +00:00
|
|
|
/// thread, it will just fall back to the global stream.
|
|
|
|
///
|
|
|
|
/// However, if the actual I/O causes an error, this function does panic.
|
2018-02-28 17:59:12 +00:00
|
|
|
fn print_to<T>(
|
2019-03-01 08:34:11 +00:00
|
|
|
args: fmt::Arguments<'_>,
|
2019-12-22 22:42:04 +00:00
|
|
|
local_s: &'static LocalKey<RefCell<Option<Box<dyn Write + Send>>>>,
|
2018-02-28 17:59:12 +00:00
|
|
|
global_s: fn() -> T,
|
|
|
|
label: &str,
|
2019-12-22 22:42:04 +00:00
|
|
|
) where
|
2018-02-28 17:59:12 +00:00
|
|
|
T: Write,
|
|
|
|
{
|
2019-12-22 22:42:04 +00:00
|
|
|
let result = local_s
|
|
|
|
.try_with(|s| {
|
2020-03-12 20:03:48 +00:00
|
|
|
// Note that we completely remove a local sink to write to in case
|
|
|
|
// our printing recursively panics/prints, so the recursive
|
|
|
|
// panic/print goes to the global sink instead of our local sink.
|
|
|
|
let prev = s.borrow_mut().take();
|
|
|
|
if let Some(mut w) = prev {
|
|
|
|
let result = w.write_fmt(args);
|
|
|
|
*s.borrow_mut() = Some(w);
|
|
|
|
return result;
|
2018-02-28 17:59:12 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
global_s().write_fmt(args)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| global_s().write_fmt(args));
|
2018-02-28 17:59:12 +00:00
|
|
|
|
2015-03-27 23:25:49 +00:00
|
|
|
if let Err(e) = result {
|
2017-04-13 14:48:09 +00:00
|
|
|
panic!("failed printing to {}: {}", label, e);
|
2015-03-08 22:30:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 16:03:17 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
#[unstable(
|
|
|
|
feature = "print_internals",
|
|
|
|
reason = "implementation detail which may disappear or be replaced at any time",
|
|
|
|
issue = "none"
|
|
|
|
)]
|
2017-04-13 14:48:09 +00:00
|
|
|
#[doc(hidden)]
|
2019-02-14 12:36:01 +00:00
|
|
|
#[cfg(not(test))]
|
2019-03-01 08:34:11 +00:00
|
|
|
pub fn _print(args: fmt::Arguments<'_>) {
|
2017-04-13 14:48:09 +00:00
|
|
|
print_to(args, &LOCAL_STDOUT, stdout, "stdout");
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
#[unstable(
|
|
|
|
feature = "print_internals",
|
|
|
|
reason = "implementation detail which may disappear or be replaced at any time",
|
|
|
|
issue = "none"
|
|
|
|
)]
|
2017-01-21 18:38:11 +00:00
|
|
|
#[doc(hidden)]
|
2019-02-14 12:36:01 +00:00
|
|
|
#[cfg(not(test))]
|
2019-03-01 08:34:11 +00:00
|
|
|
pub fn _eprint(args: fmt::Arguments<'_>) {
|
2017-04-13 14:48:09 +00:00
|
|
|
print_to(args, &LOCAL_STDERR, stderr, "stderr");
|
2017-01-21 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 12:36:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub use realstd::io::{_eprint, _print};
|