aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/io
diff options
context:
space:
mode:
authorFenrir <[email protected]>2018-04-14 20:02:05 -0600
committerFenrir <[email protected]>2018-04-21 16:35:01 -0600
commitb330206f5590d88a2f995321d2ea847ded951d1d (patch)
tree4fecd0ca00b754c494e96b13e9837db48de93109 /ctr-std/src/io
parentMove more implementation details to `imp` module (diff)
downloadctru-rs-b330206f5590d88a2f995321d2ea847ded951d1d.tar.xz
ctru-rs-b330206f5590d88a2f995321d2ea847ded951d1d.zip
Update for Rust nightly 2018-04-19
Diffstat (limited to 'ctr-std/src/io')
-rw-r--r--ctr-std/src/io/buffered.rs245
-rw-r--r--ctr-std/src/io/cursor.rs20
-rw-r--r--ctr-std/src/io/error.rs8
-rw-r--r--ctr-std/src/io/mod.rs622
-rw-r--r--ctr-std/src/io/stdio.rs148
-rw-r--r--ctr-std/src/io/util.rs15
6 files changed, 562 insertions, 496 deletions
diff --git a/ctr-std/src/io/buffered.rs b/ctr-std/src/io/buffered.rs
index 4e7db5f..d6eac74 100644
--- a/ctr-std/src/io/buffered.rs
+++ b/ctr-std/src/io/buffered.rs
@@ -25,26 +25,32 @@ use memchr;
/// results in a system call. A `BufReader` performs large, infrequent reads on
/// the underlying [`Read`] and maintains an in-memory buffer of the results.
///
+/// `BufReader` can improve the speed of programs that make *small* and
+/// *repeated* read calls to the same file or network socket. It does not
+/// help when reading very large amounts at once, or reading just one or a few
+/// times. It also provides no advantage when reading from a source that is
+/// already in memory, like a `Vec<u8>`.
+///
/// [`Read`]: ../../std/io/trait.Read.html
/// [`TcpStream::read`]: ../../std/net/struct.TcpStream.html#method.read
/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::io::prelude::*;
/// use std::io::BufReader;
/// use std::fs::File;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let f = File::open("log.txt")?;
-/// let mut reader = BufReader::new(f);
+/// fn main() -> std::io::Result<()> {
+/// let f = File::open("log.txt")?;
+/// let mut reader = BufReader::new(f);
///
-/// let mut line = String::new();
-/// let len = reader.read_line(&mut line)?;
-/// println!("First line is {} bytes long", len);
-/// # Ok(())
-/// # }
+/// let mut line = String::new();
+/// let len = reader.read_line(&mut line)?;
+/// println!("First line is {} bytes long", len);
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub struct BufReader<R> {
@@ -59,15 +65,15 @@ impl<R: Read> BufReader<R> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::BufReader;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f = File::open("log.txt")?;
- /// let reader = BufReader::new(f);
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let reader = BufReader::new(f);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(inner: R) -> BufReader<R> {
@@ -80,15 +86,15 @@ impl<R: Read> BufReader<R> {
///
/// Creating a buffer with ten bytes of capacity:
///
- /// ```
+ /// ```no_run
/// use std::io::BufReader;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f = File::open("log.txt")?;
- /// let reader = BufReader::with_capacity(10, f);
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let reader = BufReader::with_capacity(10, f);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> {
@@ -111,17 +117,17 @@ impl<R: Read> BufReader<R> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::BufReader;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f1 = File::open("log.txt")?;
- /// let reader = BufReader::new(f1);
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let reader = BufReader::new(f1);
///
- /// let f2 = reader.get_ref();
- /// # Ok(())
- /// # }
+ /// let f2 = reader.get_ref();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &R { &self.inner }
@@ -132,17 +138,17 @@ impl<R: Read> BufReader<R> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::BufReader;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f1 = File::open("log.txt")?;
- /// let mut reader = BufReader::new(f1);
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let mut reader = BufReader::new(f1);
///
- /// let f2 = reader.get_mut();
- /// # Ok(())
- /// # }
+ /// let f2 = reader.get_mut();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
@@ -150,26 +156,55 @@ impl<R: Read> BufReader<R> {
/// Returns `true` if there are no bytes in the internal buffer.
///
/// # Examples
- /// ```
+ //
+ /// ```no_run
/// # #![feature(bufreader_is_empty)]
/// use std::io::BufReader;
/// use std::io::BufRead;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f1 = File::open("log.txt")?;
- /// let mut reader = BufReader::new(f1);
- /// assert!(reader.is_empty());
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let mut reader = BufReader::new(f1);
+ /// assert!(reader.is_empty());
///
- /// if reader.fill_buf()?.len() > 0 {
- /// assert!(!reader.is_empty());
+ /// if reader.fill_buf()?.len() > 0 {
+ /// assert!(!reader.is_empty());
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
#[unstable(feature = "bufreader_is_empty", issue = "45323", reason = "recently added")]
+ #[rustc_deprecated(since = "1.26.0", reason = "use .buffer().is_empty() instead")]
pub fn is_empty(&self) -> bool {
- self.pos == self.cap
+ self.buffer().is_empty()
+ }
+
+ /// Returns a reference to the internally buffered data.
+ ///
+ /// Unlike `fill_buf`, this will not attempt to fill the buffer if it is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// # #![feature(bufreader_buffer)]
+ /// use std::io::{BufReader, BufRead};
+ /// use std::fs::File;
+ ///
+ /// fn main() -> std::io::Result<()> {
+ /// let f = File::open("log.txt")?;
+ /// let mut reader = BufReader::new(f);
+ /// assert!(reader.buffer().is_empty());
+ ///
+ /// if reader.fill_buf()?.len() > 0 {
+ /// assert!(!reader.buffer().is_empty());
+ /// }
+ /// Ok(())
+ /// }
+ /// ```
+ #[unstable(feature = "bufreader_buffer", issue = "45323")]
+ pub fn buffer(&self) -> &[u8] {
+ &self.buf[self.pos..self.cap]
}
/// Unwraps this `BufReader`, returning the underlying reader.
@@ -178,17 +213,17 @@ impl<R: Read> BufReader<R> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::BufReader;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let f1 = File::open("log.txt")?;
- /// let reader = BufReader::new(f1);
+ /// fn main() -> std::io::Result<()> {
+ /// let f1 = File::open("log.txt")?;
+ /// let reader = BufReader::new(f1);
///
- /// let f2 = reader.into_inner();
- /// # Ok(())
- /// # }
+ /// let f2 = reader.into_inner();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_inner(self) -> R { self.inner }
@@ -293,7 +328,7 @@ impl<R: Seek> Seek for BufReader<R> {
/// where `n` minus the internal buffer length overflows an `i64`, two
/// seeks will be performed instead of one. If the second seek returns
/// `Err`, the underlying reader will be left at the same position it would
- /// have if you seeked to `SeekFrom::Current(0)`.
+ /// have if you called `seek` with `SeekFrom::Current(0)`.
///
/// [`seek_relative`]: #method.seek_relative
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
@@ -330,6 +365,12 @@ impl<R: Seek> Seek for BufReader<R> {
/// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying
/// writer in large, infrequent batches.
///
+/// `BufWriter` can improve the speed of programs that make *small* and
+/// *repeated* write calls to the same file or network socket. It does not
+/// help when writing very large amounts at once, or writing just one or a few
+/// times. It also provides no advantage when writing to a destination that is
+/// in memory, like a `Vec<u8>`.
+///
/// When the `BufWriter` is dropped, the contents of its buffer will be written
/// out. However, any errors that happen in the process of flushing the buffer
/// when the writer is dropped will be ignored. Code that wishes to handle such
@@ -696,34 +737,34 @@ impl<W> fmt::Display for IntoInnerError<W> {
/// We can use `LineWriter` to write one line at a time, significantly
/// reducing the number of actual writes to the file.
///
-/// ```
+/// ```no_run
/// use std::fs::File;
/// use std::io::prelude::*;
/// use std::io::LineWriter;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let road_not_taken = b"I shall be telling this with a sigh
+/// fn main() -> std::io::Result<()> {
+/// let road_not_taken = b"I shall be telling this with a sigh
/// Somewhere ages and ages hence:
/// Two roads diverged in a wood, and I -
/// I took the one less traveled by,
/// And that has made all the difference.";
///
-/// let file = File::create("poem.txt")?;
-/// let mut file = LineWriter::new(file);
+/// let file = File::create("poem.txt")?;
+/// let mut file = LineWriter::new(file);
///
-/// for &byte in road_not_taken.iter() {
-/// file.write(&[byte]).unwrap();
-/// }
+/// for &byte in road_not_taken.iter() {
+/// file.write(&[byte]).unwrap();
+/// }
///
-/// // let's check we did the right thing.
-/// let mut file = File::open("poem.txt")?;
-/// let mut contents = String::new();
+/// // let's check we did the right thing.
+/// let mut file = File::open("poem.txt")?;
+/// let mut contents = String::new();
///
-/// file.read_to_string(&mut contents)?;
+/// file.read_to_string(&mut contents)?;
///
-/// assert_eq!(contents.as_bytes(), &road_not_taken[..]);
-/// # Ok(())
-/// # }
+/// assert_eq!(contents.as_bytes(), &road_not_taken[..]);
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub struct LineWriter<W: Write> {
@@ -736,15 +777,15 @@ impl<W: Write> LineWriter<W> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let file = File::create("poem.txt")?;
- /// let file = LineWriter::new(file);
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let file = LineWriter::new(file);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(inner: W) -> LineWriter<W> {
@@ -757,15 +798,15 @@ impl<W: Write> LineWriter<W> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let file = File::create("poem.txt")?;
- /// let file = LineWriter::with_capacity(100, file);
- /// # Ok(())
- /// # }
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let file = LineWriter::with_capacity(100, file);
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
@@ -779,17 +820,17 @@ impl<W: Write> LineWriter<W> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let file = File::create("poem.txt")?;
- /// let file = LineWriter::new(file);
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let file = LineWriter::new(file);
///
- /// let reference = file.get_ref();
- /// # Ok(())
- /// # }
+ /// let reference = file.get_ref();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_ref(&self) -> &W { self.inner.get_ref() }
@@ -801,18 +842,18 @@ impl<W: Write> LineWriter<W> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let file = File::create("poem.txt")?;
- /// let mut file = LineWriter::new(file);
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
+ /// let mut file = LineWriter::new(file);
///
- /// // we can use reference just like file
- /// let reference = file.get_mut();
- /// # Ok(())
- /// # }
+ /// // we can use reference just like file
+ /// let reference = file.get_mut();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
@@ -827,18 +868,18 @@ impl<W: Write> LineWriter<W> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::fs::File;
/// use std::io::LineWriter;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let file = File::create("poem.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let file = File::create("poem.txt")?;
///
- /// let writer: LineWriter<File> = LineWriter::new(file);
+ /// let writer: LineWriter<File> = LineWriter::new(file);
///
- /// let file: File = writer.into_inner()?;
- /// # Ok(())
- /// # }
+ /// let file: File = writer.into_inner()?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
diff --git a/ctr-std/src/io/cursor.rs b/ctr-std/src/io/cursor.rs
index 76bcb5f..2673f3c 100644
--- a/ctr-std/src/io/cursor.rs
+++ b/ctr-std/src/io/cursor.rs
@@ -10,7 +10,6 @@
use io::prelude::*;
-use core::convert::TryInto;
use cmp;
use io::{self, Initializer, SeekFrom, Error, ErrorKind};
@@ -260,9 +259,26 @@ fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> io::Result<us
Ok(amt)
}
+/// Compensate removal of some impls per
+/// https://github.com/rust-lang/rust/pull/49305#issuecomment-376293243
+#[cfg(any(target_pointer_width = "16",
+ target_pointer_width = "32"))]
+fn try_into(n: u64) -> Result<usize, ()> {
+ if n <= (<usize>::max_value() as u64) {
+ Ok(n as usize)
+ } else {
+ Err(())
+ }
+}
+
+#[cfg(any(target_pointer_width = "64"))]
+fn try_into(n: u64) -> Result<usize, ()> {
+ Ok(n as usize)
+}
+
// Resizing write implementation
fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> {
- let pos: usize = (*pos_mut).try_into().map_err(|_| {
+ let pos: usize = try_into(*pos_mut).map_err(|_| {
Error::new(ErrorKind::InvalidInput,
"cursor position exceeds maximum possible vector length")
})?;
diff --git a/ctr-std/src/io/error.rs b/ctr-std/src/io/error.rs
index f0b41f3..bdd675e 100644
--- a/ctr-std/src/io/error.rs
+++ b/ctr-std/src/io/error.rs
@@ -292,8 +292,8 @@ impl Error {
/// # if cfg!(target_os = "linux") {
/// use std::io;
///
- /// let error = io::Error::from_raw_os_error(98);
- /// assert_eq!(error.kind(), io::ErrorKind::AddrInUse);
+ /// let error = io::Error::from_raw_os_error(22);
+ /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
/// # }
/// ```
///
@@ -303,8 +303,8 @@ impl Error {
/// # if cfg!(windows) {
/// use std::io;
///
- /// let error = io::Error::from_raw_os_error(10048);
- /// assert_eq!(error.kind(), io::ErrorKind::AddrInUse);
+ /// let error = io::Error::from_raw_os_error(10022);
+ /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
/// # }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
diff --git a/ctr-std/src/io/mod.rs b/ctr-std/src/io/mod.rs
index 33d11eb..b02e133 100644
--- a/ctr-std/src/io/mod.rs
+++ b/ctr-std/src/io/mod.rs
@@ -24,21 +24,21 @@
//! example, [`Read`] adds a [`read`][`Read::read`] method, which we can use on
//! [`File`]s:
//!
-//! ```
+//! ```no_run
//! use std::io;
//! use std::io::prelude::*;
//! use std::fs::File;
//!
-//! # fn foo() -> io::Result<()> {
-//! let mut f = File::open("foo.txt")?;
-//! let mut buffer = [0; 10];
+//! fn main() -> io::Result<()> {
+//! let mut f = File::open("foo.txt")?;
+//! let mut buffer = [0; 10];
//!
-//! // read up to 10 bytes
-//! f.read(&mut buffer)?;
+//! // read up to 10 bytes
+//! f.read(&mut buffer)?;
//!
-//! println!("The bytes: {:?}", buffer);
-//! # Ok(())
-//! # }
+//! println!("The bytes: {:?}", buffer);
+//! Ok(())
+//! }
//! ```
//!
//! [`Read`] and [`Write`] are so important, implementors of the two traits have a
@@ -52,25 +52,25 @@
//! how the reading happens. [`Seek`] lets you control where the next byte is
//! coming from:
//!
-//! ```
+//! ```no_run
//! use std::io;
//! use std::io::prelude::*;
//! use std::io::SeekFrom;
//! use std::fs::File;
//!
-//! # fn foo() -> io::Result<()> {
-//! let mut f = File::open("foo.txt")?;
-//! let mut buffer = [0; 10];
+//! fn main() -> io::Result<()> {
+//! let mut f = File::open("foo.txt")?;
+//! let mut buffer = [0; 10];
//!
-//! // skip to the last 10 bytes of the file
-//! f.seek(SeekFrom::End(-10))?;
+//! // skip to the last 10 bytes of the file
+//! f.seek(SeekFrom::End(-10))?;
//!
-//! // read up to 10 bytes
-//! f.read(&mut buffer)?;
+//! // read up to 10 bytes
+//! f.read(&mut buffer)?;
//!
-//! println!("The bytes: {:?}", buffer);
-//! # Ok(())
-//! # }
+//! println!("The bytes: {:?}", buffer);
+//! Ok(())
+//! }
//! ```
//!
//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
@@ -87,70 +87,70 @@
//! For example, [`BufReader`] works with the [`BufRead`] trait to add extra
//! methods to any reader:
//!
-//! ```
+//! ```no_run
//! use std::io;
//! use std::io::prelude::*;
//! use std::io::BufReader;
//! use std::fs::File;
//!
-//! # fn foo() -> io::Result<()> {
-//! let f = File::open("foo.txt")?;
-//! let mut reader = BufReader::new(f);
-//! let mut buffer = String::new();
+//! fn main() -> io::Result<()> {
+//! let f = File::open("foo.txt")?;
+//! let mut reader = BufReader::new(f);
+//! let mut buffer = String::new();
//!
-//! // read a line into buffer
-//! reader.read_line(&mut buffer)?;
+//! // read a line into buffer
+//! reader.read_line(&mut buffer)?;
//!
-//! println!("{}", buffer);
-//! # Ok(())
-//! # }
+//! println!("{}", buffer);
+//! Ok(())
+//! }
//! ```
//!
//! [`BufWriter`] doesn't add any new ways of writing; it just buffers every call
//! to [`write`][`Write::write`]:
//!
-//! ```
+//! ```no_run
//! use std::io;
//! use std::io::prelude::*;
//! use std::io::BufWriter;
//! use std::fs::File;
//!
-//! # fn foo() -> io::Result<()> {
-//! let f = File::create("foo.txt")?;
-//! {
-//! let mut writer = BufWriter::new(f);
+//! fn main() -> io::Result<()> {
+//! let f = File::create("foo.txt")?;
+//! {
+//! let mut writer = BufWriter::new(f);
//!
-//! // write a byte to the buffer
-//! writer.write(&[42])?;
+//! // write a byte to the buffer
+//! writer.write(&[42])?;
//!
-//! } // the buffer is flushed once writer goes out of scope
+//! } // the buffer is flushed once writer goes out of scope
//!
-//! # Ok(())
-//! # }
+//! Ok(())
+//! }
//! ```
//!
//! ## Standard input and output
//!
//! A very common source of input is standard input:
//!
-//! ```
+//! ```no_run
//! use std::io;
//!
-//! # fn foo() -> io::Result<()> {
-//! let mut input = String::new();
+//! fn main() -> io::Result<()> {
+//! let mut input = String::new();
//!
-//! io::stdin().read_line(&mut input)?;
+//! io::stdin().read_line(&mut input)?;
//!
-//! println!("You typed: {}", input.trim());
-//! # Ok(())
-//! # }
+//! println!("You typed: {}", input.trim());
+//! Ok(())
+//! }
//! ```
//!
//! Note that you cannot use the [`?` operator] in functions that do not return
//! a [`Result<T, E>`][`Result`] (e.g. `main`). Instead, you can call [`.unwrap()`]
//! or `match` on the return value to catch any possible errors:
//!
-//! ```
+//! ```no_run
//! use std::io;
//!
//! let mut input = String::new();
@@ -160,14 +160,14 @@
//!
//! And a very common source of output is standard output:
//!
-//! ```
+//! ```no_run
//! use std::io;
//! use std::io::prelude::*;
//!
-//! # fn foo() -> io::Result<()> {
-//! io::stdout().write(&[42])?;
-//! # Ok(())
-//! # }
+//! fn main() -> io::Result<()> {
+//! io::stdout().write(&[42])?;
+//! Ok(())
+//! }
//! ```
//!
//! Of course, using [`io::stdout`] directly is less common than something like
@@ -179,22 +179,21 @@
//! ways of iterating over I/O. For example, [`Lines`] is used to split over
//! lines:
//!
-//! ```
+//! ```no_run
//! use std::io;
//! use std::io::prelude::*;
//! use std::io::BufReader;
//! use std::fs::File;
//!
-//! # fn foo() -> io::Result<()> {
-//! let f = File::open("foo.txt")?;
-//! let reader = BufReader::new(f);
+//! fn main() -> io::Result<()> {
+//! let f = File::open("foo.txt")?;
+//! let reader = BufReader::new(f);
//!
-//! for line in reader.lines() {
-//! println!("{}", line?);
+//! for line in reader.lines() {
+//! println!("{}", line?);
+//! }
+//! Ok(())
//! }
-//!
-//! # Ok(())
-//! # }
//! ```
//!
//! ## Functions
@@ -203,13 +202,13 @@
//! features. For example, we can use three of these functions to copy everything
//! from standard input to standard output:
//!
-//! ```
+//! ```no_run
//! use std::io;
//!
-//! # fn foo() -> io::Result<()> {
-//! io::copy(&mut io::stdin(), &mut io::stdout())?;
-//! # Ok(())
-//! # }
+//! fn main() -> io::Result<()> {
+//! io::copy(&mut io::stdin(), &mut io::stdout())?;
+//! Ok(())
+//! }
//! ```
//!
//! [functions-list]: #functions-1
@@ -416,47 +415,47 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
///
/// [`File`]s implement `Read`:
///
-/// ```
-/// # use std::io;
+/// ```no_run
+/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
-/// # fn foo() -> io::Result<()> {
-/// let mut f = File::open("foo.txt")?;
-/// let mut buffer = [0; 10];
+/// fn main() -> io::Result<()> {
+/// let mut f = File::open("foo.txt")?;
+/// let mut buffer = [0; 10];
///
-/// // read up to 10 bytes
-/// f.read(&mut buffer)?;
+/// // read up to 10 bytes
+/// f.read(&mut buffer)?;
///
-/// let mut buffer = vec![0; 10];
-/// // read the whole file
-/// f.read_to_end(&mut buffer)?;
+/// let mut buffer = vec![0; 10];
+/// // read the whole file
+/// f.read_to_end(&mut buffer)?;
///
-/// // read into a String, so that you don't need to do the conversion.
-/// let mut buffer = String::new();
-/// f.read_to_string(&mut buffer)?;
+/// // read into a String, so that you don't need to do the conversion.
+/// let mut buffer = String::new();
+/// f.read_to_string(&mut buffer)?;
///
-/// // and more! See the other methods for more details.
-/// # Ok(())
-/// # }
+/// // and more! See the other methods for more details.
+/// Ok(())
+/// }
/// ```
///
/// Read from [`&str`] because [`&[u8]`][slice] implements `Read`:
///
-/// ```
+/// ```no_run
/// # use std::io;
/// use std::io::prelude::*;
///
-/// # fn foo() -> io::Result<()> {
-/// let mut b = "This string will be read".as_bytes();
-/// let mut buffer = [0; 10];
+/// fn main() -> io::Result<()> {
+/// let mut b = "This string will be read".as_bytes();
+/// let mut buffer = [0; 10];
///
-/// // read up to 10 bytes
-/// b.read(&mut buffer)?;
+/// // read up to 10 bytes
+/// b.read(&mut buffer)?;
///
-/// // etc... it works exactly as a File does!
-/// # Ok(())
-/// # }
+/// // etc... it works exactly as a File does!
+/// Ok(())
+/// }
/// ```
///
/// [`read()`]: trait.Read.html#tymethod.read
@@ -509,19 +508,19 @@ pub trait Read {
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
/// [`File`]: ../fs/struct.File.html
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let mut buffer = [0; 10];
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = [0; 10];
///
- /// // read up to 10 bytes
- /// f.read(&mut buffer[..])?;
- /// # Ok(())
- /// # }
+ /// // read up to 10 bytes
+ /// f.read(&mut buffer[..])?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
@@ -582,20 +581,25 @@ pub trait Read {
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
/// [`File`]: ../fs/struct.File.html
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let mut buffer = Vec::new();
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = Vec::new();
///
- /// // read the whole file
- /// f.read_to_end(&mut buffer)?;
- /// # Ok(())
- /// # }
+ /// // read the whole file
+ /// f.read_to_end(&mut buffer)?;
+ /// Ok(())
+ /// }
/// ```
+ ///
+ /// (See also the [`std::fs::read`] convenience function for reading from a
+ /// file.)
+ ///
+ /// [`std::fs::read`]: ../fs/fn.read.html
#[stable(feature = "rust1", since = "1.0.0")]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
read_to_end(self, buf)
@@ -621,19 +625,24 @@ pub trait Read {
///
/// [file]: ../fs/struct.File.html
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let mut buffer = String::new();
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = String::new();
///
- /// f.read_to_string(&mut buffer)?;
- /// # Ok(())
- /// # }
+ /// f.read_to_string(&mut buffer)?;
+ /// Ok(())
+ /// }
/// ```
+ ///
+ /// (See also the [`std::fs::read_to_string`] convenience function for
+ /// reading from a file.)
+ ///
+ /// [`std::fs::read_to_string`]: ../fs/fn.read_to_string.html
#[stable(feature = "rust1", since = "1.0.0")]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
// Note that we do *not* call `.read_to_end()` here. We are passing
@@ -683,19 +692,19 @@ pub trait Read {
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
/// [`ErrorKind::UnexpectedEof`]: ../../std/io/enum.ErrorKind.html#variant.UnexpectedEof
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let mut buffer = [0; 10];
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = [0; 10];
///
- /// // read exactly 10 bytes
- /// f.read_exact(&mut buffer)?;
- /// # Ok(())
- /// # }
+ /// // read exactly 10 bytes
+ /// f.read_exact(&mut buffer)?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "read_exact", since = "1.6.0")]
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
@@ -726,28 +735,28 @@ pub trait Read {
///
/// [file]: ../fs/struct.File.html
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::Read;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let mut buffer = Vec::new();
- /// let mut other_buffer = Vec::new();
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = Vec::new();
+ /// let mut other_buffer = Vec::new();
///
- /// {
- /// let reference = f.by_ref();
+ /// {
+ /// let reference = f.by_ref();
///
- /// // read at most 5 bytes
- /// reference.take(5).read_to_end(&mut buffer)?;
+ /// // read at most 5 bytes
+ /// reference.take(5).read_to_end(&mut buffer)?;
///
- /// } // drop our &mut reference so we can use f again
+ /// } // drop our &mut reference so we can use f again
///
- /// // original file still usable, read the rest
- /// f.read_to_end(&mut other_buffer)?;
- /// # Ok(())
- /// # }
+ /// // original file still usable, read the rest
+ /// f.read_to_end(&mut other_buffer)?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
@@ -772,19 +781,19 @@ pub trait Read {
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
/// [`None`]: ../../std/option/enum.Option.html#variant.None
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
///
- /// for byte in f.bytes() {
- /// println!("{}", byte.unwrap());
+ /// for byte in f.bytes() {
+ /// println!("{}", byte.unwrap());
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn bytes(self) -> Bytes<Self> where Self: Sized {
@@ -812,20 +821,20 @@ pub trait Read {
/// [`char`]: ../../std/primitive.char.html
/// [`None`]: ../../std/option/enum.Option.html#variant.None
///
- /// ```
+ /// ```no_run
/// #![feature(io)]
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
///
- /// for c in f.chars() {
- /// println!("{}", c.unwrap());
+ /// for c in f.chars() {
+ /// println!("{}", c.unwrap());
+ /// }
+ /// Ok(())
/// }
- /// # Ok(())
- /// # }
/// ```
#[unstable(feature = "io", reason = "the semantics of a partial read/write \
of where errors happen is currently \
@@ -847,23 +856,23 @@ pub trait Read {
///
/// [file]: ../fs/struct.File.html
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f1 = File::open("foo.txt")?;
- /// let mut f2 = File::open("bar.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut f1 = File::open("foo.txt")?;
+ /// let mut f2 = File::open("bar.txt")?;
///
- /// let mut handle = f1.chain(f2);
- /// let mut buffer = String::new();
+ /// let mut handle = f1.chain(f2);
+ /// let mut buffer = String::new();
///
- /// // read the value into a String. We could use any Read method here,
- /// // this is just one example.
- /// handle.read_to_string(&mut buffer)?;
- /// # Ok(())
- /// # }
+ /// // read the value into a String. We could use any Read method here,
+ /// // this is just one example.
+ /// handle.read_to_string(&mut buffer)?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
@@ -885,21 +894,21 @@ pub trait Read {
/// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
/// [`read()`]: trait.Read.html#tymethod.read
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- /// let mut buffer = [0; 5];
+ /// fn main() -> io::Result<()> {
+ /// let mut f = File::open("foo.txt")?;
+ /// let mut buffer = [0; 5];
///
- /// // read at most five bytes
- /// let mut handle = f.take(5);
+ /// // read at most five bytes
+ /// let mut handle = f.take(5);
///
- /// handle.read(&mut buffer)?;
- /// # Ok(())
- /// # }
+ /// handle.read(&mut buffer)?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn take(self, limit: u64) -> Take<Self> where Self: Sized {
@@ -974,16 +983,16 @@ impl Initializer {
///
/// # Examples
///
-/// ```
+/// ```no_run
/// use std::io::prelude::*;
/// use std::fs::File;
///
-/// # fn foo() -> std::io::Result<()> {
-/// let mut buffer = File::create("foo.txt")?;
+/// fn main() -> std::io::Result<()> {
+/// let mut buffer = File::create("foo.txt")?;
///
-/// buffer.write(b"some bytes")?;
-/// # Ok(())
-/// # }
+/// buffer.write(b"some bytes")?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(spotlight)]
@@ -1022,17 +1031,17 @@ pub trait Write {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut buffer = File::create("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
///
- /// // Writes some prefix of the byte string, not necessarily all of it.
- /// buffer.write(b"some bytes")?;
- /// # Ok(())
- /// # }
+ /// // Writes some prefix of the byte string, not necessarily all of it.
+ /// buffer.write(b"some bytes")?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn write(&mut self, buf: &[u8]) -> Result<usize>;
@@ -1047,18 +1056,18 @@ pub trait Write {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::prelude::*;
/// use std::io::BufWriter;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut buffer = BufWriter::new(File::create("foo.txt")?);
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = BufWriter::new(File::create("foo.txt")?);
///
- /// buffer.write(b"some bytes")?;
- /// buffer.flush()?;
- /// # Ok(())
- /// # }
+ /// buffer.write(b"some bytes")?;
+ /// buffer.flush()?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn flush(&mut self) -> Result<()>;
@@ -1082,16 +1091,16 @@ pub trait Write {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut buffer = File::create("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
///
- /// buffer.write_all(b"some bytes")?;
- /// # Ok(())
- /// # }
+ /// buffer.write_all(b"some bytes")?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
@@ -1131,19 +1140,19 @@ pub trait Write {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut buffer = File::create("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
///
- /// // this call
- /// write!(buffer, "{:.*}", 2, 1.234567)?;
- /// // turns into this:
- /// buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
- /// # Ok(())
- /// # }
+ /// // this call
+ /// write!(buffer, "{:.*}", 2, 1.234567)?;
+ /// // turns into this:
+ /// buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()> {
@@ -1187,19 +1196,19 @@ pub trait Write {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::Write;
/// use std::fs::File;
///
- /// # fn foo() -> std::io::Result<()> {
- /// let mut buffer = File::create("foo.txt")?;
+ /// fn main() -> std::io::Result<()> {
+ /// let mut buffer = File::create("foo.txt")?;
///
- /// let reference = buffer.by_ref();
+ /// let reference = buffer.by_ref();
///
- /// // we can use reference just like our original buffer
- /// reference.write_all(b"some bytes")?;
- /// # Ok(())
- /// # }
+ /// // we can use reference just like our original buffer
+ /// reference.write_all(b"some bytes")?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
@@ -1217,19 +1226,19 @@ pub trait Write {
///
/// [file]: ../fs/struct.File.html
///
-/// ```
+/// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
/// use std::io::SeekFrom;
///
-/// # fn foo() -> io::Result<()> {
-/// let mut f = File::open("foo.txt")?;
+/// fn main() -> io::Result<()> {
+/// let mut f = File::open("foo.txt")?;
///
-/// // move the cursor 42 bytes from the start of the file
-/// f.seek(SeekFrom::Start(42))?;
-/// # Ok(())
-/// # }
+/// // move the cursor 42 bytes from the start of the file
+/// f.seek(SeekFrom::Start(42))?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Seek {
@@ -1320,7 +1329,7 @@ fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
///
/// A locked standard input implements `BufRead`:
///
-/// ```
+/// ```no_run
/// use std::io;
/// use std::io::prelude::*;
///
@@ -1342,21 +1351,21 @@ fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
/// [`lines`]: #method.lines
/// [`Read`]: trait.Read.html
///
-/// ```
+/// ```no_run
/// use std::io::{self, BufReader};
/// use std::io::prelude::*;
/// use std::fs::File;
///
-/// # fn foo() -> io::Result<()> {
-/// let f = File::open("foo.txt")?;
-/// let f = BufReader::new(f);
+/// fn main() -> io::Result<()> {
+/// let f = File::open("foo.txt")?;
+/// let f = BufReader::new(f);
///
-/// for line in f.lines() {
-/// println!("{}", line.unwrap());
-/// }
+/// for line in f.lines() {
+/// println!("{}", line.unwrap());
+/// }
///
-/// # Ok(())
-/// # }
+/// Ok(())
+/// }
/// ```
///
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1383,7 +1392,7 @@ pub trait BufRead: Read {
///
/// A locked standard input implements `BufRead`:
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
///
@@ -1437,8 +1446,6 @@ pub trait BufRead: Read {
///
/// If successful, this function will return the total number of bytes read.
///
- /// An empty buffer returned indicates that the stream has reached EOF.
- ///
/// # Errors
///
/// This function will ignore all instances of [`ErrorKind::Interrupted`] and
@@ -1508,6 +1515,8 @@ pub trait BufRead: Read {
/// error is encountered then `buf` may contain some bytes already read in
/// the event that all data read so far was valid UTF-8.
///
+ /// [`read_until`]: #method.read_until
+ ///
/// # Examples
///
/// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
@@ -1645,19 +1654,19 @@ impl<T, U> Chain<T, U> {
///
/// # Examples
///
- /// ```
- /// # use std::io;
+ /// ```no_run
+ /// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut foo_file = File::open("foo.txt")?;
- /// let mut bar_file = File::open("bar.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut foo_file = File::open("foo.txt")?;
+ /// let mut bar_file = File::open("bar.txt")?;
///
- /// let chain = foo_file.chain(bar_file);
- /// let (foo_file, bar_file) = chain.into_inner();
- /// # Ok(())
- /// # }
+ /// let chain = foo_file.chain(bar_file);
+ /// let (foo_file, bar_file) = chain.into_inner();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn into_inner(self) -> (T, U) {
@@ -1668,19 +1677,19 @@ impl<T, U> Chain<T, U> {
///
/// # Examples
///
- /// ```
- /// # use std::io;
+ /// ```no_run
+ /// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut foo_file = File::open("foo.txt")?;
- /// let mut bar_file = File::open("bar.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut foo_file = File::open("foo.txt")?;
+ /// let mut bar_file = File::open("bar.txt")?;
///
- /// let chain = foo_file.chain(bar_file);
- /// let (foo_file, bar_file) = chain.get_ref();
- /// # Ok(())
- /// # }
+ /// let chain = foo_file.chain(bar_file);
+ /// let (foo_file, bar_file) = chain.get_ref();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn get_ref(&self) -> (&T, &U) {
@@ -1695,19 +1704,19 @@ impl<T, U> Chain<T, U> {
///
/// # Examples
///
- /// ```
- /// # use std::io;
+ /// ```no_run
+ /// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut foo_file = File::open("foo.txt")?;
- /// let mut bar_file = File::open("bar.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut foo_file = File::open("foo.txt")?;
+ /// let mut bar_file = File::open("bar.txt")?;
///
- /// let mut chain = foo_file.chain(bar_file);
- /// let (foo_file, bar_file) = chain.get_mut();
- /// # Ok(())
- /// # }
+ /// let mut chain = foo_file.chain(bar_file);
+ /// let (foo_file, bar_file) = chain.get_mut();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn get_mut(&mut self) -> (&mut T, &mut U) {
@@ -1794,20 +1803,20 @@ impl<T> Take<T> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let f = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let f = File::open("foo.txt")?;
///
- /// // read at most five bytes
- /// let handle = f.take(5);
+ /// // read at most five bytes
+ /// let handle = f.take(5);
///
- /// println!("limit: {}", handle.limit());
- /// # Ok(())
- /// # }
+ /// println!("limit: {}", handle.limit());
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn limit(&self) -> u64 { self.limit }
@@ -1819,24 +1828,23 @@ impl<T> Take<T> {
///
/// # Examples
///
- /// ```
- /// #![feature(take_set_limit)]
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let f = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let f = File::open("foo.txt")?;
///
- /// // read at most five bytes
- /// let mut handle = f.take(5);
- /// handle.set_limit(10);
+ /// // read at most five bytes
+ /// let mut handle = f.take(5);
+ /// handle.set_limit(10);
///
- /// assert_eq!(handle.limit(), 10);
- /// # Ok(())
- /// # }
+ /// assert_eq!(handle.limit(), 10);
+ /// Ok(())
+ /// }
/// ```
- #[unstable(feature = "take_set_limit", issue = "42781")]
+ #[stable(feature = "take_set_limit", since = "1.27.0")]
pub fn set_limit(&mut self, limit: u64) {
self.limit = limit;
}
@@ -1845,21 +1853,21 @@ impl<T> Take<T> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut file = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
///
- /// let mut buffer = [0; 5];
- /// let mut handle = file.take(5);
- /// handle.read(&mut buffer)?;
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// handle.read(&mut buffer)?;
///
- /// let file = handle.into_inner();
- /// # Ok(())
- /// # }
+ /// let file = handle.into_inner();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "io_take_into_inner", since = "1.15.0")]
pub fn into_inner(self) -> T {
@@ -1870,21 +1878,21 @@ impl<T> Take<T> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut file = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
///
- /// let mut buffer = [0; 5];
- /// let mut handle = file.take(5);
- /// handle.read(&mut buffer)?;
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// handle.read(&mut buffer)?;
///
- /// let file = handle.get_ref();
- /// # Ok(())
- /// # }
+ /// let file = handle.get_ref();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn get_ref(&self) -> &T {
@@ -1899,21 +1907,21 @@ impl<T> Take<T> {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io;
/// use std::io::prelude::*;
/// use std::fs::File;
///
- /// # fn foo() -> io::Result<()> {
- /// let mut file = File::open("foo.txt")?;
+ /// fn main() -> io::Result<()> {
+ /// let mut file = File::open("foo.txt")?;
///
- /// let mut buffer = [0; 5];
- /// let mut handle = file.take(5);
- /// handle.read(&mut buffer)?;
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// handle.read(&mut buffer)?;
///
- /// let file = handle.get_mut();
- /// # Ok(())
- /// # }
+ /// let file = handle.get_mut();
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
pub fn get_mut(&mut self) -> &mut T {
diff --git a/ctr-std/src/io/stdio.rs b/ctr-std/src/io/stdio.rs
index 831688b..2472bed 100644
--- a/ctr-std/src/io/stdio.rs
+++ b/ctr-std/src/io/stdio.rs
@@ -17,7 +17,7 @@ use io::{self, Initializer, BufReader, LineWriter};
use sync::{Arc, Mutex, MutexGuard};
use sys::stdio;
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
-use thread::{LocalKey, LocalKeyState};
+use thread::LocalKey;
/// Stdout used by print! and println! macros
thread_local! {
@@ -171,29 +171,29 @@ pub struct StdinLock<'a> {
///
/// Using implicit synchronization:
///
-/// ```
+/// ```no_run
/// use std::io::{self, Read};
///
-/// # fn foo() -> io::Result<String> {
-/// let mut buffer = String::new();
-/// io::stdin().read_to_string(&mut buffer)?;
-/// # Ok(buffer)
-/// # }
+/// fn main() -> io::Result<()> {
+/// let mut buffer = String::new();
+/// io::stdin().read_to_string(&mut buffer)?;
+/// Ok(())
+/// }
/// ```
///
/// Using explicit synchronization:
///
-/// ```
+/// ```no_run
/// use std::io::{self, Read};
///
-/// # fn foo() -> io::Result<String> {
-/// let mut buffer = String::new();
-/// let stdin = io::stdin();
-/// let mut handle = stdin.lock();
+/// fn main() -> io::Result<()> {
+/// let mut buffer = String::new();
+/// let stdin = io::stdin();
+/// let mut handle = stdin.lock();
///
-/// handle.read_to_string(&mut buffer)?;
-/// # Ok(buffer)
-/// # }
+/// handle.read_to_string(&mut buffer)?;
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn stdin() -> Stdin {
@@ -225,17 +225,17 @@ impl Stdin {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::{self, Read};
///
- /// # fn foo() -> io::Result<String> {
- /// let mut buffer = String::new();
- /// let stdin = io::stdin();
- /// let mut handle = stdin.lock();
+ /// fn main() -> io::Result<()> {
+ /// let mut buffer = String::new();
+ /// let stdin = io::stdin();
+ /// let mut handle = stdin.lock();
///
- /// handle.read_to_string(&mut buffer)?;
- /// # Ok(buffer)
- /// # }
+ /// handle.read_to_string(&mut buffer)?;
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn lock(&self) -> StdinLock {
@@ -369,29 +369,29 @@ pub struct StdoutLock<'a> {
///
/// Using implicit synchronization:
///
-/// ```
+/// ```no_run
/// use std::io::{self, Write};
///
-/// # fn foo() -> io::Result<()> {
-/// io::stdout().write(b"hello world")?;
+/// fn main() -> io::Result<()> {
+/// io::stdout().write(b"hello world")?;
///
-/// # Ok(())
-/// # }
+/// Ok(())
+/// }
/// ```
///
/// Using explicit synchronization:
///
-/// ```
+/// ```no_run
/// use std::io::{self, Write};
///
-/// # fn foo() -> io::Result<()> {
-/// let stdout = io::stdout();
-/// let mut handle = stdout.lock();
+/// fn main() -> io::Result<()> {
+/// let stdout = io::stdout();
+/// let mut handle = stdout.lock();
///
-/// handle.write(b"hello world")?;
+/// handle.write(b"hello world")?;
///
-/// # Ok(())
-/// # }
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn stdout() -> Stdout {
@@ -419,17 +419,17 @@ impl Stdout {
///
/// # Examples
///
- /// ```
+ /// ```no_run
/// use std::io::{self, Write};
///
- /// # fn foo() -> io::Result<()> {
- /// let stdout = io::stdout();
- /// let mut handle = stdout.lock();
+ /// fn main() -> io::Result<()> {
+ /// let stdout = io::stdout();
+ /// let mut handle = stdout.lock();
///
- /// handle.write(b"hello world")?;
+ /// handle.write(b"hello world")?;
///
- /// # Ok(())
- /// # }
+ /// Ok(())
+ /// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn lock(&self) -> StdoutLock {
@@ -505,29 +505,29 @@ pub struct StderrLock<'a> {
///
/// Using implicit synchronization:
///
-/// ```
+/// ```no_run
/// use std::io::{self, Write};
///
-/// # fn foo() -> io::Result<()> {
-/// io::stderr().write(b"hello world")?;
+/// fn main() -> io::Result<()> {
+/// io::stderr().write(b"hello world")?;
///
-/// # Ok(())
-/// # }
+/// Ok(())
+/// }
/// ```
///
/// Using explicit synchronization:
///
-/// ```
+/// ```no_run
/// use std::io::{self, Write};
///
-/// # fn foo() -> io::Result<()> {
-/// let stderr = io::stderr();
-/// let mut handle = stderr.lock();
+/// fn main() -> io::Result<()> {
+/// let stderr = io::stderr();
+/// let mut handle = stderr.lock();
///
-/// handle.write(b"hello world")?;
+/// handle.write(b"hello world")?;
///
-/// # Ok(())
-/// # }
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn stderr() -> Stderr {
@@ -663,29 +663,31 @@ pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
///
/// This function is used to print error messages, so it takes extra
/// care to avoid causing a panic when `local_stream` is unusable.
-/// For instance, if the TLS key for the local stream is uninitialized
-/// or already destroyed, or if the local stream is locked by another
+/// For instance, if the TLS key for the local stream is
+/// already destroyed, or if the local stream is locked by another
/// thread, it will just fall back to the global stream.
///
/// However, if the actual I/O causes an error, this function does panic.
-fn print_to<T>(args: fmt::Arguments,
- local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
- global_s: fn() -> T,
- label: &str) where T: Write {
- let result = match local_s.state() {
- LocalKeyState::Uninitialized |
- LocalKeyState::Destroyed => global_s().write_fmt(args),
- LocalKeyState::Valid => {
- local_s.with(|s| {
- if let Ok(mut borrowed) = s.try_borrow_mut() {
- if let Some(w) = borrowed.as_mut() {
- return w.write_fmt(args);
- }
- }
- global_s().write_fmt(args)
- })
+fn print_to<T>(
+ args: fmt::Arguments,
+ local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
+ global_s: fn() -> T,
+ label: &str,
+)
+where
+ T: Write,
+{
+ let result = local_s.try_with(|s| {
+ if let Ok(mut borrowed) = s.try_borrow_mut() {
+ if let Some(w) = borrowed.as_mut() {
+ return w.write_fmt(args);
+ }
}
- };
+ global_s().write_fmt(args)
+ }).unwrap_or_else(|_| {
+ global_s().write_fmt(args)
+ });
+
if let Err(e) = result {
panic!("failed printing to {}: {}", label, e);
}
diff --git a/ctr-std/src/io/util.rs b/ctr-std/src/io/util.rs
index 45d281e..195310a 100644
--- a/ctr-std/src/io/util.rs
+++ b/ctr-std/src/io/util.rs
@@ -34,16 +34,15 @@ use mem;
/// ```
/// use std::io;
///
-/// # fn foo() -> io::Result<()> {
-/// let mut reader: &[u8] = b"hello";
-/// let mut writer: Vec<u8> = vec![];
+/// fn main() -> io::Result<()> {
+/// let mut reader: &[u8] = b"hello";
+/// let mut writer: Vec<u8> = vec![];
///
-/// io::copy(&mut reader, &mut writer)?;
+/// io::copy(&mut reader, &mut writer)?;
///
-/// assert_eq!(&b"hello"[..], &writer[..]);
-/// # Ok(())
-/// # }
-/// # foo().unwrap();
+/// assert_eq!(&b"hello"[..], &writer[..]);
+/// Ok(())
+/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>