2015-02-24 23:27:20 -08:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use io::prelude::*;
|
|
|
|
|
2016-12-14 13:02:00 -08:00
|
|
|
use cell::RefCell;
|
2015-02-24 23:27:20 -08:00
|
|
|
use fmt;
|
|
|
|
use io::lazy::Lazy;
|
|
|
|
use io::{self, BufReader, LineWriter};
|
|
|
|
use sync::{Arc, Mutex, MutexGuard};
|
|
|
|
use sys::stdio;
|
2015-04-04 00:46:54 +03:00
|
|
|
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
2015-10-31 09:41:21 -07:00
|
|
|
use thread::LocalKeyState;
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2015-03-19 00:48:08 -04:00
|
|
|
/// Stdout used by print! and println! macros
|
2015-03-09 00:30:15 +02:00
|
|
|
thread_local! {
|
|
|
|
static LOCAL_STDOUT: RefCell<Option<Box<Write + Send>>> = {
|
|
|
|
RefCell::new(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 23:27:20 -08: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-14 18:08:09 -07:00
|
|
|
/// the `std::io::stdio::stdin_raw` function.
|
|
|
|
struct StdinRaw(stdio::Stdin);
|
2015-02-24 23:27:20 -08: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-14 18:08:09 -07:00
|
|
|
/// the `std::io::stdio::stdout_raw` function.
|
|
|
|
struct StdoutRaw(stdio::Stdout);
|
2015-02-24 23:27:20 -08: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-14 18:08:09 -07:00
|
|
|
/// the `std::io::stdio::stderr_raw` function.
|
|
|
|
struct StderrRaw(stdio::Stderr);
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Constructs a new raw handle to the standard input of this process.
|
2015-02-24 23:27:20 -08: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.
|
2015-06-09 21:39:36 -07:00
|
|
|
fn stdin_raw() -> io::Result<StdinRaw> { stdio::Stdin::new().map(StdinRaw) }
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2015-08-11 20:58:15 -04:00
|
|
|
/// Constructs a new raw handle to the standard output stream of this process.
|
2015-02-24 23:27:20 -08: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-11 20:58:15 -04:00
|
|
|
/// `std::io::stdout` handles so writes which happen via this raw handle may
|
2015-02-24 23:27:20 -08:00
|
|
|
/// appear before previous writes.
|
|
|
|
///
|
|
|
|
/// The returned handle has no external synchronization or buffering layered on
|
|
|
|
/// top.
|
2015-06-09 21:39:36 -07:00
|
|
|
fn stdout_raw() -> io::Result<StdoutRaw> { stdio::Stdout::new().map(StdoutRaw) }
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2015-08-11 20:58:15 -04:00
|
|
|
/// Constructs a new raw handle to the standard error stream of this process.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
|
|
|
/// The returned handle does not interact with any other handles created nor
|
2015-08-11 20:58:15 -04:00
|
|
|
/// handles returned by `std::io::stderr`.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
|
|
|
/// The returned handle has no external synchronization or buffering layered on
|
|
|
|
/// top.
|
2015-06-09 21:39:36 -07:00
|
|
|
fn stderr_raw() -> io::Result<StderrRaw> { stdio::Stderr::new().map(StderrRaw) }
|
2015-02-24 23:27:20 -08:00
|
|
|
|
|
|
|
impl Read for StdinRaw {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) }
|
2016-02-12 00:17:24 -08:00
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
self.0.read_to_end(buf)
|
|
|
|
}
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
impl Write for StdoutRaw {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }
|
2016-11-10 19:33:59 -07:00
|
|
|
fn flush(&mut self) -> io::Result<()> { self.0.flush() }
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
impl Write for StderrRaw {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }
|
2016-11-10 19:33:59 -07:00
|
|
|
fn flush(&mut self) -> io::Result<()> { self.0.flush() }
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-06-09 21:39:36 -07:00
|
|
|
enum Maybe<T> {
|
|
|
|
Real(T),
|
|
|
|
Fake,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: io::Write> io::Write for Maybe<W> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
match *self {
|
|
|
|
Maybe::Real(ref mut w) => handle_ebadf(w.write(buf), buf.len()),
|
|
|
|
Maybe::Fake => Ok(buf.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
match *self {
|
|
|
|
Maybe::Real(ref mut w) => handle_ebadf(w.flush(), ()),
|
|
|
|
Maybe::Fake => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: io::Read> io::Read for Maybe<R> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
match *self {
|
2016-01-24 21:14:56 -08:00
|
|
|
Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), 0),
|
2015-06-09 21:39:36 -07:00
|
|
|
Maybe::Fake => Ok(0)
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 00:17:24 -08:00
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
match *self {
|
|
|
|
Maybe::Real(ref mut r) => handle_ebadf(r.read_to_end(buf), 0),
|
|
|
|
Maybe::Fake => Ok(0)
|
|
|
|
}
|
|
|
|
}
|
2015-06-09 21:39:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
|
2016-09-22 00:29:00 +00:00
|
|
|
use sys::stdio::EBADF_ERR;
|
2015-06-09 21:39:36 -07:00
|
|
|
|
|
|
|
match r {
|
2016-09-22 00:29:00 +00:00
|
|
|
Err(ref e) if e.raw_os_error() == Some(EBADF_ERR) => Ok(default),
|
2015-06-09 21:39:36 -07:00
|
|
|
r => r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 23:27:20 -08: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 12:41:04 +02:00
|
|
|
/// process. A handle can be `lock`'d to gain full access to [`BufRead`] methods
|
2016-03-23 23:39:01 +00:00
|
|
|
/// (e.g. `.lines()`). Reads to this handle are otherwise locked with respect
|
|
|
|
/// to other reads.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
|
|
|
/// This handle implements the `Read` trait, but beware that concurrent reads
|
|
|
|
/// of `Stdin` must be executed with care.
|
2015-04-22 13:57:08 -07:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// Created by the [`io::stdin`] method.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`io::stdin`]: fn.stdin.html
|
|
|
|
/// [`BufRead`]: trait.BufRead.html
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub struct Stdin {
|
2015-06-09 21:39:36 -07:00
|
|
|
inner: Arc<Mutex<BufReader<Maybe<StdinRaw>>>>,
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
/// A locked reference to the `Stdin` handle.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// This handle implements both the [`Read`] and [`BufRead`] traits, and
|
|
|
|
/// is constructed via the [`Stdin::lock`] method.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`Read`]: trait.Read.html
|
|
|
|
/// [`BufRead`]: trait.BufRead.html
|
|
|
|
/// [`Stdin::lock`]: struct.Stdin.html#method.lock
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub struct StdinLock<'a> {
|
2015-06-09 21:39:36 -07:00
|
|
|
inner: MutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-07-08 18:31:08 -04:00
|
|
|
/// Constructs a new handle to the standard input of the current process.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-07-08 18:31:08 -04: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
|
2015-12-30 12:41:04 +02:00
|
|
|
/// locking, see the [`lock() method`][lock].
|
2015-07-08 18:31:08 -04:00
|
|
|
///
|
|
|
|
/// [lock]: struct.Stdin.html#method.lock
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using implicit synchronization:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<String> {
|
|
|
|
/// let mut buffer = String::new();
|
2016-12-28 14:32:35 +05:30
|
|
|
/// io::stdin().read_to_string(&mut buffer)?;
|
2015-07-08 18:31:08 -04:00
|
|
|
/// # Ok(buffer)
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using explicit synchronization:
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-07-08 18:31:08 -04:00
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<String> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut handle = stdin.lock();
|
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// handle.read_to_string(&mut buffer)?;
|
2015-07-08 18:31:08 -04:00
|
|
|
/// # Ok(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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn stdin() -> Stdin {
|
2015-06-09 21:39:36 -07:00
|
|
|
static INSTANCE: Lazy<Mutex<BufReader<Maybe<StdinRaw>>>> = Lazy::new(stdin_init);
|
2015-02-24 23:27:20 -08:00
|
|
|
return Stdin {
|
|
|
|
inner: INSTANCE.get().expect("cannot access stdin during shutdown"),
|
|
|
|
};
|
|
|
|
|
2015-06-09 21:39:36 -07:00
|
|
|
fn stdin_init() -> Arc<Mutex<BufReader<Maybe<StdinRaw>>>> {
|
|
|
|
let stdin = match stdin_raw() {
|
|
|
|
Ok(stdin) => Maybe::Real(stdin),
|
|
|
|
_ => Maybe::Fake
|
|
|
|
};
|
|
|
|
|
2016-09-30 21:01:53 +00:00
|
|
|
Arc::new(Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stdin {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Locks this handle to the standard input stream, returning a readable
|
2015-02-24 23:27:20 -08:00
|
|
|
/// guard.
|
|
|
|
///
|
|
|
|
/// The lock is released when the returned lock goes out of scope. The
|
2015-12-30 12:41:04 +02:00
|
|
|
/// returned guard also implements the [`Read`] and [`BufRead`] traits for
|
2015-02-24 23:27:20 -08:00
|
|
|
/// accessing the underlying data.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 21:01:42 +02:00
|
|
|
/// [`Read`]: trait.Read.html
|
|
|
|
/// [`BufRead`]: trait.BufRead.html
|
2016-07-30 00:56:14 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<String> {
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut handle = stdin.lock();
|
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// handle.read_to_string(&mut buffer)?;
|
2016-07-30 00:56:14 +02:00
|
|
|
/// # Ok(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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn lock(&self) -> StdinLock {
|
2015-03-17 23:05:44 -07:00
|
|
|
StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
|
2015-02-24 23:27:20 -08: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 14:16:46 -07:00
|
|
|
|
|
|
|
/// Locks this handle and reads a line of input into the specified buffer.
|
|
|
|
///
|
|
|
|
/// For detailed semantics of this method, see the documentation on
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`BufRead::read_line`].
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`BufRead::read_line`]: trait.BufRead.html#method.read_line
|
2015-06-27 22:58:49 +02: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:
|
|
|
|
///
|
|
|
|
/// - Pipe some text to it, e.g. `printf foo | path/to/executable`
|
|
|
|
/// - Give it text interactively by running the executable directly,
|
2015-12-23 17:46:59 +02:00
|
|
|
/// in which case it will wait for the Enter key to be pressed before
|
2015-06-27 22:58:49 +02: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-07-08 21:53:45 +02: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 14:16:46 -07:00
|
|
|
self.lock().read_line(buf)
|
|
|
|
}
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Stdin {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl Read for Stdin {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.lock().read(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 14:16:46 -07:00
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
2015-02-24 23:27:20 -08: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 14:16:46 -07:00
|
|
|
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
|
2015-02-24 23:27:20 -08:00
|
|
|
self.lock().read_to_string(buf)
|
|
|
|
}
|
2015-07-20 00:23:37 -03:00
|
|
|
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
|
|
|
|
self.lock().read_exact(buf)
|
|
|
|
}
|
2015-02-24 23:27:20 -08: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl<'a> Read for StdinLock<'a> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
2015-07-10 17:34:07 +01:00
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
2016-02-12 00:17:24 -08:00
|
|
|
self.inner.read_to_end(buf)
|
2015-07-10 17:34:07 +01:00
|
|
|
}
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
2015-06-09 21:39:36 -07: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl<'a> BufRead for StdinLock<'a> {
|
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> { self.inner.fill_buf() }
|
|
|
|
fn consume(&mut self, n: usize) { self.inner.consume(n) }
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl<'a> fmt::Debug for StdinLock<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("StdinLock { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 23:27:20 -08: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 14:04:52 -04:00
|
|
|
/// over locking is available via the [`lock`] method.
|
2015-04-22 13:57:08 -07:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// Created by the [`io::stdout`] method.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`lock`]: #method.lock
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`io::stdout`]: fn.stdout.html
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08: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.
|
2015-06-09 21:39:36 -07:00
|
|
|
inner: Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>,
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
/// A locked reference to the `Stdout` handle.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// This handle implements the [`Write`] trait, and is constructed via
|
|
|
|
/// the [`Stdout::lock`] method.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`Write`]: trait.Write.html
|
|
|
|
/// [`Stdout::lock`]: struct.Stdout.html#method.lock
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub struct StdoutLock<'a> {
|
2015-06-09 21:39:36 -07:00
|
|
|
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<Maybe<StdoutRaw>>>>,
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-07-08 18:31:08 -04:00
|
|
|
/// Constructs a new handle to the standard output of the current process.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
|
|
|
/// Each handle returned is a reference to a shared global buffer whose access
|
2015-07-08 18:31:08 -04:00
|
|
|
/// is synchronized via a mutex. If you need more explicit control over
|
2015-12-24 02:52:27 +02:00
|
|
|
/// locking, see the [Stdout::lock] method.
|
2015-07-08 18:31:08 -04:00
|
|
|
///
|
2015-12-24 02:52:27 +02:00
|
|
|
/// [Stdout::lock]: struct.Stdout.html#method.lock
|
2015-07-08 18:31:08 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using implicit synchronization:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
2016-12-28 14:32:35 +05:30
|
|
|
/// io::stdout().write(b"hello world")?;
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-07-08 18:31:08 -04:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using explicit synchronization:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let stdout = io::stdout();
|
|
|
|
/// let mut handle = stdout.lock();
|
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// handle.write(b"hello world")?;
|
2015-07-08 18:31:08 -04: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn stdout() -> Stdout {
|
2015-06-09 21:39:36 -07:00
|
|
|
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>
|
|
|
|
= Lazy::new(stdout_init);
|
2015-02-24 23:27:20 -08:00
|
|
|
return Stdout {
|
|
|
|
inner: INSTANCE.get().expect("cannot access stdout during shutdown"),
|
|
|
|
};
|
|
|
|
|
2015-06-09 21:39:36 -07:00
|
|
|
fn stdout_init() -> Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> {
|
|
|
|
let stdout = match stdout_raw() {
|
|
|
|
Ok(stdout) => Maybe::Real(stdout),
|
|
|
|
_ => Maybe::Fake,
|
|
|
|
};
|
|
|
|
Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout))))
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stdout {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Locks this handle to the standard output stream, returning a writable
|
2015-02-24 23:27:20 -08: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-30 00:57:20 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let stdout = io::stdout();
|
|
|
|
/// let mut handle = stdout.lock();
|
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// handle.write(b"hello world")?;
|
2016-07-30 00:57:20 +02: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn lock(&self) -> StdoutLock {
|
2015-03-17 23:05:44 -07:00
|
|
|
StdoutLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Stdout {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl Write for Stdout {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.lock().write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.lock().flush()
|
|
|
|
}
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
|
|
|
self.lock().write_all(buf)
|
|
|
|
}
|
2015-04-04 00:46:54 +03:00
|
|
|
fn write_fmt(&mut self, args: fmt::Arguments) -> io::Result<()> {
|
|
|
|
self.lock().write_fmt(args)
|
|
|
|
}
|
2015-02-24 23:27:20 -08: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl<'a> Write for StdoutLock<'a> {
|
|
|
|
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-04 00:46:54 +03:00
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().flush()
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl<'a> fmt::Debug for StdoutLock<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("StdoutLock { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 23:27:20 -08:00
|
|
|
/// A handle to the standard error stream of a process.
|
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// For more information, see the [`io::stderr`] method.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`io::stderr`]: fn.stderr.html
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub struct Stderr {
|
2015-06-09 21:39:36 -07:00
|
|
|
inner: Arc<ReentrantMutex<RefCell<Maybe<StderrRaw>>>>,
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
/// A locked reference to the `Stderr` handle.
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-12-24 02:52:27 +02:00
|
|
|
/// This handle implements the `Write` trait and is constructed via
|
2015-12-30 12:41:04 +02:00
|
|
|
/// the [`Stderr::lock`] method.
|
2015-12-24 02:52:27 +02:00
|
|
|
///
|
2015-12-30 12:41:04 +02:00
|
|
|
/// [`Stderr::lock`]: struct.Stderr.html#method.lock
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub struct StderrLock<'a> {
|
2015-06-09 21:39:36 -07:00
|
|
|
inner: ReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
|
2015-07-08 18:31:08 -04:00
|
|
|
/// Constructs a new handle to the standard error of the current process.
|
|
|
|
///
|
|
|
|
/// This handle is not buffered.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using implicit synchronization:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
2016-12-28 14:32:35 +05:30
|
|
|
/// io::stderr().write(b"hello world")?;
|
2015-07-08 18:31:08 -04:00
|
|
|
///
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using explicit synchronization:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let stderr = io::stderr();
|
|
|
|
/// let mut handle = stderr.lock();
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// handle.write(b"hello world")?;
|
2015-02-24 23:27:20 -08:00
|
|
|
///
|
2015-07-08 18:31:08 -04: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn stderr() -> Stderr {
|
2015-06-09 21:39:36 -07:00
|
|
|
static INSTANCE: Lazy<ReentrantMutex<RefCell<Maybe<StderrRaw>>>> = Lazy::new(stderr_init);
|
2015-02-24 23:27:20 -08:00
|
|
|
return Stderr {
|
|
|
|
inner: INSTANCE.get().expect("cannot access stderr during shutdown"),
|
|
|
|
};
|
|
|
|
|
2015-06-09 21:39:36 -07:00
|
|
|
fn stderr_init() -> Arc<ReentrantMutex<RefCell<Maybe<StderrRaw>>>> {
|
|
|
|
let stderr = match stderr_raw() {
|
|
|
|
Ok(stderr) => Maybe::Real(stderr),
|
|
|
|
_ => Maybe::Fake,
|
|
|
|
};
|
|
|
|
Arc::new(ReentrantMutex::new(RefCell::new(stderr)))
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stderr {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Locks this handle to the standard error stream, returning a writable
|
2015-02-24 23:27:20 -08: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-30 00:53:18 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
|
|
|
/// fn foo() -> io::Result<()> {
|
|
|
|
/// let stderr = io::stderr();
|
|
|
|
/// let mut handle = stderr.lock();
|
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// handle.write(b"hello world")?;
|
2016-07-30 00:53:18 +02: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn lock(&self) -> StderrLock {
|
2015-03-17 23:05:44 -07:00
|
|
|
StderrLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Stderr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl Write for Stderr {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.lock().write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.lock().flush()
|
|
|
|
}
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
|
|
|
self.lock().write_all(buf)
|
|
|
|
}
|
2015-04-04 00:46:54 +03:00
|
|
|
fn write_fmt(&mut self, args: fmt::Arguments) -> io::Result<()> {
|
|
|
|
self.lock().write_fmt(args)
|
|
|
|
}
|
2015-02-24 23:27:20 -08: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 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
impl<'a> Write for StderrLock<'a> {
|
|
|
|
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-04 00:46:54 +03:00
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.inner.borrow_mut().flush()
|
2015-02-24 23:27:20 -08:00
|
|
|
}
|
|
|
|
}
|
2015-03-11 15:24:14 -07:00
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl<'a> fmt::Debug for StderrLock<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("StderrLock { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:12:29 +09:00
|
|
|
/// Resets the thread-local stderr handle to the specified writer
|
2015-03-11 15:24:14 -07:00
|
|
|
///
|
2015-05-09 00:12:29 +09:00
|
|
|
/// This will replace the current thread's stderr handle, returning the old
|
2015-03-09 00:30:15 +02:00
|
|
|
/// handle. All future calls to `panic!` and friends will emit their output to
|
2015-03-11 15:24:14 -07:00
|
|
|
/// this specified handle.
|
|
|
|
///
|
2015-05-09 00:12:29 +09:00
|
|
|
/// Note that this does not need to be called for all new threads; the default
|
2015-03-09 00:30:15 +02:00
|
|
|
/// output handle is to the process's stderr stream.
|
|
|
|
#[unstable(feature = "set_stdio",
|
2015-03-11 15:24:14 -07:00
|
|
|
reason = "this function may disappear completely or be replaced \
|
2015-08-13 10:12:38 -07:00
|
|
|
with a more general mechanism",
|
|
|
|
issue = "0")]
|
2015-03-11 15:24:14 -07:00
|
|
|
#[doc(hidden)]
|
2016-09-14 17:15:48 +00:00
|
|
|
pub fn set_panic(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
|
2015-03-11 15:24:14 -07:00
|
|
|
use panicking::LOCAL_STDERR;
|
|
|
|
use mem;
|
|
|
|
LOCAL_STDERR.with(move |slot| {
|
2016-09-14 17:15:48 +00:00
|
|
|
mem::replace(&mut *slot.borrow_mut(), sink)
|
2015-03-11 15:24:14 -07:00
|
|
|
}).and_then(|mut s| {
|
|
|
|
let _ = s.flush();
|
|
|
|
Some(s)
|
|
|
|
})
|
|
|
|
}
|
2015-03-09 00:30:15 +02:00
|
|
|
|
2015-05-09 00:12:29 +09:00
|
|
|
/// Resets the thread-local stdout handle to the specified writer
|
2015-03-09 00:30:15 +02:00
|
|
|
///
|
2015-05-09 00:12:29 +09:00
|
|
|
/// This will replace the current thread's stdout handle, returning the old
|
2015-03-09 00:30:15 +02:00
|
|
|
/// handle. All future calls to `print!` and friends will emit their output to
|
|
|
|
/// this specified handle.
|
|
|
|
///
|
2015-05-09 00:12:29 +09:00
|
|
|
/// Note that this does not need to be called for all new threads; the default
|
2015-03-09 00:30:15 +02:00
|
|
|
/// output handle is to the process's stdout stream.
|
|
|
|
#[unstable(feature = "set_stdio",
|
|
|
|
reason = "this function may disappear completely or be replaced \
|
2015-08-13 10:12:38 -07:00
|
|
|
with a more general mechanism",
|
|
|
|
issue = "0")]
|
2015-03-09 00:30:15 +02:00
|
|
|
#[doc(hidden)]
|
2016-09-14 17:15:48 +00:00
|
|
|
pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
|
2015-03-09 00:30:15 +02:00
|
|
|
use mem;
|
|
|
|
LOCAL_STDOUT.with(move |slot| {
|
2016-09-14 17:15:48 +00:00
|
|
|
mem::replace(&mut *slot.borrow_mut(), sink)
|
2015-03-09 00:30:15 +02:00
|
|
|
}).and_then(|mut s| {
|
|
|
|
let _ = s.flush();
|
|
|
|
Some(s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "print",
|
2015-08-13 10:12:38 -07:00
|
|
|
reason = "implementation detail which may disappear or be replaced at any time",
|
|
|
|
issue = "0")]
|
2015-03-09 00:30:15 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn _print(args: fmt::Arguments) {
|
2015-10-31 09:41:21 -07:00
|
|
|
// As an implementation of the `println!` macro, we want to try our best to
|
|
|
|
// not panic wherever possible and get the output somewhere. There are
|
|
|
|
// currently two possible vectors for panics we take care of here:
|
|
|
|
//
|
|
|
|
// 1. If the TLS key for the local stdout has been destroyed, accessing it
|
|
|
|
// would cause a panic. Note that we just lump in the uninitialized case
|
|
|
|
// here for convenience, we're not trying to avoid a panic.
|
|
|
|
// 2. If the local stdout is currently in use (e.g. we're in the middle of
|
|
|
|
// already printing) then accessing again would cause a panic.
|
|
|
|
//
|
|
|
|
// If, however, the actual I/O causes an error, we do indeed panic.
|
|
|
|
let result = match LOCAL_STDOUT.state() {
|
|
|
|
LocalKeyState::Uninitialized |
|
|
|
|
LocalKeyState::Destroyed => stdout().write_fmt(args),
|
|
|
|
LocalKeyState::Valid => {
|
|
|
|
LOCAL_STDOUT.with(|s| {
|
2016-12-14 13:02:00 -08:00
|
|
|
if let Ok(mut borrowed) = s.try_borrow_mut() {
|
|
|
|
if let Some(w) = borrowed.as_mut() {
|
2015-10-31 09:41:21 -07:00
|
|
|
return w.write_fmt(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stdout().write_fmt(args)
|
|
|
|
})
|
2015-03-27 16:25:49 -07:00
|
|
|
}
|
2015-10-31 09:41:21 -07:00
|
|
|
};
|
2015-03-27 16:25:49 -07:00
|
|
|
if let Err(e) = result {
|
2015-03-09 00:30:15 +02:00
|
|
|
panic!("failed printing to stdout: {}", e);
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 09:03:17 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2015-04-24 17:30:41 +02:00
|
|
|
mod tests {
|
2015-03-18 09:03:17 -07:00
|
|
|
use thread;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2016-09-07 05:34:15 +00:00
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
2015-03-18 09:03:17 -07:00
|
|
|
fn panic_doesnt_poison() {
|
|
|
|
thread::spawn(|| {
|
|
|
|
let _a = stdin();
|
|
|
|
let _a = _a.lock();
|
|
|
|
let _a = stdout();
|
|
|
|
let _a = _a.lock();
|
|
|
|
let _a = stderr();
|
|
|
|
let _a = _a.lock();
|
|
|
|
panic!();
|
|
|
|
}).join().unwrap_err();
|
|
|
|
|
|
|
|
let _a = stdin();
|
|
|
|
let _a = _a.lock();
|
|
|
|
let _a = stdout();
|
|
|
|
let _a = _a.lock();
|
|
|
|
let _a = stderr();
|
|
|
|
let _a = _a.lock();
|
|
|
|
}
|
|
|
|
}
|