2015-01-31 20:24:36 -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.
|
|
|
|
|
|
|
|
//! Traits, helpers, and type definitions for core I/O functionality.
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! The `std::io` module contains a number of common things you'll need
|
|
|
|
//! when doing input and output. The most core part of this module is
|
2016-10-11 17:48:14 +02:00
|
|
|
//! the [`Read`] and [`Write`] traits, which provide the
|
2015-07-20 18:07:35 -04:00
|
|
|
//! most general interface for reading and writing input and output.
|
|
|
|
//!
|
|
|
|
//! # Read and Write
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! Because they are traits, [`Read`] and [`Write`] are implemented by a number
|
2015-10-17 12:34:25 -07:00
|
|
|
//! of other types, and you can implement them for your types too. As such,
|
|
|
|
//! you'll see a few different types of I/O throughout the documentation in
|
2016-10-11 17:48:14 +02:00
|
|
|
//! this module: [`File`]s, [`TcpStream`]s, and sometimes even [`Vec<T>`]s. For
|
2017-04-06 12:57:40 +01:00
|
|
|
//! example, [`Read`] adds a [`read`][`Read::read`] method, which we can use on
|
2017-08-10 23:11:40 +02:00
|
|
|
//! [`File`]s:
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//! use std::io::prelude::*;
|
|
|
|
//! use std::fs::File;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! let mut f = File::open("foo.txt")?;
|
|
|
|
//! let mut buffer = [0; 10];
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! // read up to 10 bytes
|
|
|
|
//! f.read(&mut buffer)?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! println!("The bytes: {:?}", buffer);
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! [`Read`] and [`Write`] are so important, implementors of the two traits have a
|
2015-07-20 18:07:35 -04:00
|
|
|
//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
|
2016-10-11 17:48:14 +02:00
|
|
|
//! of 'a type that implements the [`Read`] trait'. Much easier!
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! ## Seek and BufRead
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! Beyond that, there are two important traits that are provided: [`Seek`]
|
|
|
|
//! and [`BufRead`]. Both of these build on top of a reader to control
|
|
|
|
//! how the reading happens. [`Seek`] lets you control where the next byte is
|
2015-07-20 18:07:35 -04:00
|
|
|
//! coming from:
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//! use std::io::prelude::*;
|
|
|
|
//! use std::io::SeekFrom;
|
|
|
|
//! use std::fs::File;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! let mut f = File::open("foo.txt")?;
|
|
|
|
//! let mut buffer = [0; 10];
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! // skip to the last 10 bytes of the file
|
|
|
|
//! f.seek(SeekFrom::End(-10))?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! // read up to 10 bytes
|
|
|
|
//! f.read(&mut buffer)?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! println!("The bytes: {:?}", buffer);
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
|
2015-07-20 18:07:35 -04:00
|
|
|
//! to show it off, we'll need to talk about buffers in general. Keep reading!
|
|
|
|
//!
|
|
|
|
//! ## BufReader and BufWriter
|
|
|
|
//!
|
|
|
|
//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
|
|
|
|
//! making near-constant calls to the operating system. To help with this,
|
2016-10-11 17:48:14 +02:00
|
|
|
//! `std::io` comes with two structs, [`BufReader`] and [`BufWriter`], which wrap
|
2015-07-20 18:07:35 -04:00
|
|
|
//! readers and writers. The wrapper uses a buffer, reducing the number of
|
|
|
|
//! calls and providing nicer methods for accessing exactly what you want.
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! For example, [`BufReader`] works with the [`BufRead`] trait to add extra
|
2015-07-20 18:07:35 -04:00
|
|
|
//! methods to any reader:
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//! use std::io::prelude::*;
|
|
|
|
//! use std::io::BufReader;
|
|
|
|
//! use std::fs::File;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! let f = File::open("foo.txt")?;
|
|
|
|
//! let mut reader = BufReader::new(f);
|
|
|
|
//! let mut buffer = String::new();
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! // read a line into buffer
|
|
|
|
//! reader.read_line(&mut buffer)?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! println!("{}", buffer);
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! [`BufWriter`] doesn't add any new ways of writing; it just buffers every call
|
2017-04-06 12:57:40 +01:00
|
|
|
//! to [`write`][`Write::write`]:
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//! use std::io::prelude::*;
|
|
|
|
//! use std::io::BufWriter;
|
|
|
|
//! use std::fs::File;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! let f = File::create("foo.txt")?;
|
|
|
|
//! {
|
|
|
|
//! let mut writer = BufWriter::new(f);
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! // write a byte to the buffer
|
|
|
|
//! writer.write(&[42])?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! } // the buffer is flushed once writer goes out of scope
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Standard input and output
|
|
|
|
//!
|
|
|
|
//! A very common source of input is standard input:
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! let mut input = String::new();
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! io::stdin().read_line(&mut input)?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! println!("You typed: {}", input.trim());
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2017-08-10 23:11:40 +02:00
|
|
|
//! Note that you cannot use the [`?` operator] in functions that do not return
|
2018-06-19 18:32:44 -04:00
|
|
|
//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
|
2017-08-10 23:11:40 +02:00
|
|
|
//! or `match` on the return value to catch any possible errors:
|
2017-03-27 16:34:13 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2017-03-28 13:27:46 -04:00
|
|
|
//! use std::io;
|
|
|
|
//!
|
2017-03-23 13:17:21 -04:00
|
|
|
//! let mut input = String::new();
|
2017-03-27 16:34:13 -04:00
|
|
|
//!
|
|
|
|
//! io::stdin().read_line(&mut input).unwrap();
|
2017-03-23 13:17:21 -04:00
|
|
|
//! ```
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! And a very common source of output is standard output:
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//! use std::io::prelude::*;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! io::stdout().write(&[42])?;
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2017-03-12 14:04:52 -04:00
|
|
|
//! Of course, using [`io::stdout`] directly is less common than something like
|
2016-10-11 17:48:14 +02:00
|
|
|
//! [`println!`].
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! ## Iterator types
|
|
|
|
//!
|
|
|
|
//! A large number of the structures provided by `std::io` are for various
|
2016-10-11 17:48:14 +02:00
|
|
|
//! ways of iterating over I/O. For example, [`Lines`] is used to split over
|
2015-07-20 18:07:35 -04:00
|
|
|
//! lines:
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//! use std::io::prelude::*;
|
|
|
|
//! use std::io::BufReader;
|
|
|
|
//! use std::fs::File;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! let f = File::open("foo.txt")?;
|
|
|
|
//! let reader = BufReader::new(f);
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! for line in reader.lines() {
|
|
|
|
//! println!("{}", line?);
|
|
|
|
//! }
|
|
|
|
//! Ok(())
|
2015-07-20 18:07:35 -04:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Functions
|
|
|
|
//!
|
2016-05-02 15:54:54 +01:00
|
|
|
//! There are a number of [functions][functions-list] that offer access to various
|
2015-07-20 18:07:35 -04:00
|
|
|
//! features. For example, we can use three of these functions to copy everything
|
|
|
|
//! from standard input to standard output:
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! ```no_run
|
2015-07-20 18:07:35 -04:00
|
|
|
//! use std::io;
|
|
|
|
//!
|
2018-03-24 22:56:07 -04:00
|
|
|
//! fn main() -> io::Result<()> {
|
|
|
|
//! io::copy(&mut io::stdin(), &mut io::stdout())?;
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
2015-07-20 18:07:35 -04:00
|
|
|
//! ```
|
|
|
|
//!
|
2017-09-01 18:15:10 +12:00
|
|
|
//! [functions-list]: #functions-1
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! ## io::Result
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! Last, but certainly not least, is [`io::Result`]. This type is used
|
2015-07-20 18:07:35 -04:00
|
|
|
//! as the return type of many `std::io` functions that can cause an error, and
|
2016-12-29 14:12:14 +05:30
|
|
|
//! can be returned from your own functions as well. Many of the examples in this
|
|
|
|
//! module use the [`?` operator]:
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::io;
|
|
|
|
//!
|
|
|
|
//! fn read_input() -> io::Result<()> {
|
|
|
|
//! let mut input = String::new();
|
|
|
|
//!
|
2016-12-28 14:32:35 +05:30
|
|
|
//! io::stdin().read_line(&mut input)?;
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
|
|
|
//! println!("You typed: {}", input.trim());
|
|
|
|
//!
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2016-10-11 17:48:14 +02:00
|
|
|
//! The return type of `read_input()`, [`io::Result<()>`][`io::Result`], is a very
|
|
|
|
//! common type for functions which don't have a 'real' return value, but do want to
|
|
|
|
//! return errors if they happen. In this case, the only purpose of this function is
|
2015-10-07 23:11:25 +01:00
|
|
|
//! to read the line and print it, so we use `()`.
|
2015-07-20 18:07:35 -04:00
|
|
|
//!
|
2015-11-30 21:23:19 -05:00
|
|
|
//! ## Platform-specific behavior
|
|
|
|
//!
|
|
|
|
//! Many I/O functions throughout the standard library are documented to indicate
|
|
|
|
//! what various library or syscalls they are delegated to. This is done to help
|
|
|
|
//! applications both understand what's happening under the hood as well as investigate
|
|
|
|
//! any possibly unclear semantics. Note, however, that this is informative, not a binding
|
|
|
|
//! contract. The implementation of many of these functions are subject to change over
|
|
|
|
//! time and may call fewer or more syscalls/library functions.
|
2016-10-11 17:48:14 +02:00
|
|
|
//!
|
|
|
|
//! [`Read`]: trait.Read.html
|
|
|
|
//! [`Write`]: trait.Write.html
|
|
|
|
//! [`Seek`]: trait.Seek.html
|
|
|
|
//! [`BufRead`]: trait.BufRead.html
|
|
|
|
//! [`File`]: ../fs/struct.File.html
|
|
|
|
//! [`TcpStream`]: ../net/struct.TcpStream.html
|
|
|
|
//! [`Vec<T>`]: ../vec/struct.Vec.html
|
|
|
|
//! [`BufReader`]: struct.BufReader.html
|
|
|
|
//! [`BufWriter`]: struct.BufWriter.html
|
2017-04-06 12:57:40 +01:00
|
|
|
//! [`Write::write`]: trait.Write.html#tymethod.write
|
2017-03-12 14:04:52 -04:00
|
|
|
//! [`io::stdout`]: fn.stdout.html
|
2016-10-11 17:48:14 +02:00
|
|
|
//! [`println!`]: ../macro.println.html
|
|
|
|
//! [`Lines`]: struct.Lines.html
|
|
|
|
//! [`io::Result`]: type.Result.html
|
2017-06-13 01:30:08 +09:00
|
|
|
//! [`?` operator]: ../../book/first-edition/syntax-index.html
|
2017-04-06 12:57:40 +01:00
|
|
|
//! [`Read::read`]: trait.Read.html#tymethod.read
|
2017-08-10 23:11:40 +02:00
|
|
|
//! [`Result`]: ../result/enum.Result.html
|
|
|
|
//! [`.unwrap()`]: ../result/enum.Result.html#method.unwrap
|
2015-01-31 20:24:36 -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-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
use cmp;
|
|
|
|
use fmt;
|
2015-03-10 23:13:24 -05:00
|
|
|
use str;
|
2015-12-15 00:03:42 +01:00
|
|
|
use memchr;
|
2017-05-14 21:29:18 -04:00
|
|
|
use ptr;
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-08-11 17:27:05 -07:00
|
|
|
pub use self::buffered::{BufReader, BufWriter, LineWriter};
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub use self::buffered::IntoInnerError;
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub use self::cursor::Cursor;
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub use self::error::{Result, Error, ErrorKind};
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat};
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-04-20 17:37:12 -04:00
|
|
|
pub use self::stdio::{stdin, stdout, stderr, Stdin, Stdout, Stderr};
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-24 23:27:20 -08:00
|
|
|
pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
|
2017-04-20 17:37:12 -04:00
|
|
|
#[unstable(feature = "print_internals", issue = "0")]
|
|
|
|
pub use self::stdio::{_print, _eprint};
|
2017-05-14 21:29:18 -04:00
|
|
|
#[unstable(feature = "libstd_io_internals", issue = "42788")]
|
2015-03-11 15:24:14 -07:00
|
|
|
#[doc(no_inline, hidden)]
|
2015-03-09 00:30:15 +02:00
|
|
|
pub use self::stdio::{set_panic, set_print};
|
2015-02-24 23:27:20 -08:00
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
pub mod prelude;
|
|
|
|
mod buffered;
|
|
|
|
mod cursor;
|
|
|
|
mod error;
|
|
|
|
mod impls;
|
2015-05-27 11:18:36 +03:00
|
|
|
mod lazy;
|
2015-01-31 20:24:36 -08:00
|
|
|
mod util;
|
2015-02-24 23:27:20 -08:00
|
|
|
mod stdio;
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2016-09-30 21:01:53 +00:00
|
|
|
const DEFAULT_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
struct Guard<'a> { buf: &'a mut Vec<u8>, len: usize }
|
|
|
|
|
|
|
|
impl<'a> Drop for Guard<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { self.buf.set_len(self.len); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
// A few methods below (read_to_string, read_line) will append data into a
|
|
|
|
// `String` buffer, but we need to be pretty careful when doing this. The
|
|
|
|
// implementation will just call `.as_mut_vec()` and then delegate to a
|
|
|
|
// byte-oriented reading method, but we must ensure that when returning we never
|
|
|
|
// leave `buf` in a state such that it contains invalid UTF-8 in its bounds.
|
|
|
|
//
|
|
|
|
// To this end, we use an RAII guard (to protect against panics) which updates
|
|
|
|
// the length of the string when it is dropped. This guard initially truncates
|
2015-02-04 23:00:02 -05:00
|
|
|
// the string to the prior length and only after we've validated that the
|
2015-01-31 20:24:36 -08:00
|
|
|
// new contents are valid UTF-8 do we allow it to set a longer length.
|
|
|
|
//
|
|
|
|
// The unsafety in this function is twofold:
|
|
|
|
//
|
|
|
|
// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
|
|
|
|
// checks.
|
|
|
|
// 2. We're passing a raw buffer to the function `f`, and it is expected that
|
|
|
|
// the function only *appends* bytes to the buffer. We'll get undefined
|
|
|
|
// behavior if existing bytes are overwritten to have non-UTF-8 data.
|
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 append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
|
|
|
|
where F: FnOnce(&mut Vec<u8>) -> Result<usize>
|
2015-01-31 20:24:36 -08:00
|
|
|
{
|
|
|
|
unsafe {
|
2017-05-14 21:29:18 -04:00
|
|
|
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
|
|
|
|
let ret = f(g.buf);
|
|
|
|
if str::from_utf8(&g.buf[g.len..]).is_err() {
|
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
|
|
|
ret.and_then(|_| {
|
2015-05-09 18:57:26 +03:00
|
|
|
Err(Error::new(ErrorKind::InvalidData,
|
2015-03-31 16:01:03 -07:00
|
|
|
"stream did not contain valid UTF-8"))
|
2015-01-31 20:24:36 -08:00
|
|
|
})
|
|
|
|
} else {
|
2017-05-14 21:29:18 -04:00
|
|
|
g.len = g.buf.len();
|
2015-01-31 20:24:36 -08:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 18:47:29 -07:00
|
|
|
// This uses an adaptive system to extend the vector when it fills. We want to
|
|
|
|
// avoid paying to allocate and zero a huge chunk of memory if the reader only
|
|
|
|
// has 4 bytes while still making large reads if the reader does have a ton
|
|
|
|
// of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
|
2018-08-01 10:23:07 +02:00
|
|
|
// time is 4,500 times (!) slower than a default reservation size of 32 if the
|
|
|
|
// reader has a very small amount of data to return.
|
2017-05-14 21:29:18 -04:00
|
|
|
//
|
|
|
|
// Because we're extending the buffer with uninitialized data for trusted
|
|
|
|
// readers, we need to make sure to truncate that if any of this panics.
|
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<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
|
2018-08-01 10:23:07 +02:00
|
|
|
read_to_end_with_reservation(r, buf, 32)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_to_end_with_reservation<R: Read + ?Sized>(r: &mut R,
|
|
|
|
buf: &mut Vec<u8>,
|
|
|
|
reservation_size: usize) -> Result<usize>
|
|
|
|
{
|
2015-03-28 18:47:29 -07:00
|
|
|
let start_len = buf.len();
|
2017-05-14 21:29:18 -04:00
|
|
|
let mut g = Guard { len: buf.len(), buf: buf };
|
2015-03-28 18:47:29 -07:00
|
|
|
let ret;
|
2015-01-31 20:24:36 -08:00
|
|
|
loop {
|
2017-05-14 21:29:18 -04:00
|
|
|
if g.len == g.buf.len() {
|
|
|
|
unsafe {
|
2018-08-01 10:23:07 +02:00
|
|
|
g.buf.reserve(reservation_size);
|
Optimize `read_to_end`.
This patch makes `read_to_end` use Vec's memory-growth pattern rather
than using a custom pattern.
This has some interesting effects:
- If memory is reserved up front, `read_to_end` can be faster, as it
starts reading at the buffer size, rather than always starting at 32
bytes. This speeds up file reading by 2x in one of my use cases.
- It can reduce the number of syscalls when reading large files.
Previously, `read_to_end` would settle into a sequence of 8192-byte
reads. With this patch, the read size follows Vec's allocation
pattern. For example, on a 16MiB file, it can do 21 read syscalls
instead of 2057. In simple benchmarks of large files though, overall
speed is still dominated by the actual I/O.
- A downside is that Read implementations that don't implement
`initializer()` may see increased memory zeroing overhead.
I benchmarked this on a variety of data sizes, with and without
preallocated buffers. Most benchmarks see no difference, but reading
a small/medium file with a pre-allocated buffer is faster.
2017-11-16 13:08:17 -08:00
|
|
|
let capacity = g.buf.capacity();
|
|
|
|
g.buf.set_len(capacity);
|
2017-05-14 21:29:18 -04:00
|
|
|
r.initializer().initialize(&mut g.buf[g.len..]);
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
2015-03-28 18:47:29 -07:00
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
match r.read(&mut g.buf[g.len..]) {
|
2015-03-28 18:47:29 -07:00
|
|
|
Ok(0) => {
|
2017-05-14 21:29:18 -04:00
|
|
|
ret = Ok(g.len - start_len);
|
2015-03-28 18:47:29 -07:00
|
|
|
break;
|
|
|
|
}
|
2017-05-14 21:29:18 -04:00
|
|
|
Ok(n) => g.len += n,
|
2015-01-31 20:24:36 -08:00
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
2015-03-28 18:47:29 -07:00
|
|
|
Err(e) => {
|
|
|
|
ret = Err(e);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
}
|
2015-03-28 18:47:29 -07:00
|
|
|
|
|
|
|
ret
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
2015-07-09 16:23:27 -04:00
|
|
|
/// The `Read` trait allows for reading bytes from a source.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-06-13 18:49:30 -07:00
|
|
|
/// Implementors of the `Read` trait are called 'readers'.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// Readers are defined by one required method, [`read()`]. Each call to [`read()`]
|
2015-07-09 16:23:27 -04:00
|
|
|
/// will attempt to pull bytes from this source into a provided buffer. A
|
2017-08-21 11:45:33 +02:00
|
|
|
/// number of other methods are implemented in terms of [`read()`], giving
|
2015-07-09 16:23:27 -04:00
|
|
|
/// implementors a number of ways to read bytes while only needing to implement
|
|
|
|
/// a single method.
|
|
|
|
///
|
|
|
|
/// Readers are intended to be composable with one another. Many implementors
|
2017-08-21 11:45:33 +02:00
|
|
|
/// throughout [`std::io`] take and provide types which implement the `Read`
|
2015-07-09 16:23:27 -04:00
|
|
|
/// trait.
|
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// Please note that each call to [`read()`] may involve a system call, and
|
|
|
|
/// therefore, using something that implements [`BufRead`], such as
|
|
|
|
/// [`BufReader`], will be more efficient.
|
2015-09-30 13:35:33 -04:00
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]s implement `Read`:
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
2015-07-10 13:49:12 -04:00
|
|
|
/// use std::io::prelude::*;
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = [0; 10];
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read up to 10 bytes
|
|
|
|
/// f.read(&mut buffer)?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let mut buffer = vec![0; 10];
|
|
|
|
/// // read the whole file
|
|
|
|
/// f.read_to_end(&mut buffer)?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // 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)?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // and more! See the other methods for more details.
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04:00
|
|
|
/// ```
|
2017-11-18 16:10:14 -07:00
|
|
|
///
|
2017-12-19 23:08:31 +01:00
|
|
|
/// Read from [`&str`] because [`&[u8]`][slice] implements `Read`:
|
2017-11-18 16:10:14 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2017-11-18 16:10:14 -07:00
|
|
|
/// # use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut b = "This string will be read".as_bytes();
|
|
|
|
/// let mut buffer = [0; 10];
|
2017-11-18 16:10:14 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read up to 10 bytes
|
|
|
|
/// b.read(&mut buffer)?;
|
2017-11-18 16:10:14 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // etc... it works exactly as a File does!
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-11-18 16:10:14 -07:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`read()`]: trait.Read.html#tymethod.read
|
|
|
|
/// [`std::io`]: ../../std/io/index.html
|
|
|
|
/// [`File`]: ../fs/struct.File.html
|
|
|
|
/// [`BufRead`]: trait.BufRead.html
|
|
|
|
/// [`BufReader`]: struct.BufReader.html
|
2017-12-19 23:08:31 +01:00
|
|
|
/// [`&str`]: ../../std/primitive.str.html
|
|
|
|
/// [slice]: ../../std/primitive.slice.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")]
|
2017-10-04 15:51:35 -05:00
|
|
|
#[doc(spotlight)]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub trait Read {
|
|
|
|
/// Pull some bytes from this source into the specified buffer, returning
|
|
|
|
/// how many bytes were read.
|
|
|
|
///
|
|
|
|
/// This function does not provide any guarantees about whether it blocks
|
|
|
|
/// waiting for data, but if an object needs to block for a read but cannot
|
2017-08-21 11:45:33 +02:00
|
|
|
/// it will typically signal this via an [`Err`] return value.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// If the return value of this method is [`Ok(n)`], then it must be
|
2015-01-31 20:24:36 -08:00
|
|
|
/// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
|
2015-03-25 18:02:11 +01:00
|
|
|
/// that the buffer `buf` has been filled in with `n` bytes of data from this
|
2015-01-31 20:24:36 -08:00
|
|
|
/// source. If `n` is `0`, then it can indicate one of two scenarios:
|
|
|
|
///
|
|
|
|
/// 1. This reader has reached its "end of file" and will likely no longer
|
|
|
|
/// be able to produce bytes. Note that this does not mean that the
|
|
|
|
/// reader will *always* no longer be able to produce bytes.
|
|
|
|
/// 2. The buffer specified was 0 bytes in length.
|
|
|
|
///
|
|
|
|
/// No guarantees are provided about the contents of `buf` when this
|
|
|
|
/// function is called, implementations cannot rely on any property of the
|
|
|
|
/// contents of `buf` being true. It is recommended that implementations
|
|
|
|
/// only write data to `buf` instead of reading its contents.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If this function encounters any form of I/O or other error, an error
|
|
|
|
/// variant will be returned. If an error is returned then it must be
|
|
|
|
/// guaranteed that no bytes were read.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
|
2017-04-21 10:32:13 +02:00
|
|
|
/// operation should be retried if there is nothing else to do.
|
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]s implement `Read`:
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
|
|
|
|
/// [`Ok(n)`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
|
|
|
|
/// [`File`]: ../fs/struct.File.html
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = [0; 10];
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read up to 10 bytes
|
|
|
|
/// f.read(&mut buffer[..])?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04: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-01-31 20:24:36 -08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
|
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
/// Determines if this `Read`er can work with buffers of uninitialized
|
|
|
|
/// memory.
|
|
|
|
///
|
|
|
|
/// The default implementation returns an initializer which will zero
|
|
|
|
/// buffers.
|
|
|
|
///
|
|
|
|
/// If a `Read`er guarantees that it can work properly with uninitialized
|
2017-08-21 11:45:33 +02:00
|
|
|
/// memory, it should call [`Initializer::nop()`]. See the documentation for
|
|
|
|
/// [`Initializer`] for details.
|
2017-05-14 21:29:18 -04:00
|
|
|
///
|
|
|
|
/// The behavior of this method must be independent of the state of the
|
|
|
|
/// `Read`er - the method only takes `&self` so that it can be used through
|
|
|
|
/// trait objects.
|
|
|
|
///
|
2017-08-24 17:33:36 +02:00
|
|
|
/// # Safety
|
2017-05-14 21:29:18 -04:00
|
|
|
///
|
|
|
|
/// This method is unsafe because a `Read`er could otherwise return a
|
|
|
|
/// non-zeroing `Initializer` from another `Read` type without an `unsafe`
|
|
|
|
/// block.
|
2017-08-21 11:45:33 +02:00
|
|
|
///
|
|
|
|
/// [`Initializer::nop()`]: ../../std/io/struct.Initializer.html#method.nop
|
|
|
|
/// [`Initializer`]: ../../std/io/struct.Initializer.html
|
2017-05-14 21:29:18 -04:00
|
|
|
#[unstable(feature = "read_initializer", issue = "42788")]
|
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::zeroing()
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
/// Read all bytes until EOF in this source, placing them into `buf`.
|
|
|
|
///
|
|
|
|
/// All bytes read from this source will be appended to the specified buffer
|
2017-08-21 11:45:33 +02:00
|
|
|
/// `buf`. This function will continuously call [`read()`] to append more data to
|
|
|
|
/// `buf` until [`read()`] returns either [`Ok(0)`] or an error of
|
|
|
|
/// non-[`ErrorKind::Interrupted`] kind.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-04-13 22:21:51 +03:00
|
|
|
/// If successful, this function will return the total number of bytes read.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If this function encounters an error of the kind
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`ErrorKind::Interrupted`] then the error is ignored and the operation
|
2015-01-31 20:24:36 -08:00
|
|
|
/// will continue.
|
|
|
|
///
|
|
|
|
/// If any other read error is encountered then this function immediately
|
|
|
|
/// returns. Any bytes which have already been read will be appended to
|
|
|
|
/// `buf`.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]s implement `Read`:
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`read()`]: trait.Read.html#tymethod.read
|
|
|
|
/// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
|
|
|
|
/// [`File`]: ../fs/struct.File.html
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = Vec::new();
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read the whole file
|
|
|
|
/// f.read_to_end(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04:00
|
|
|
/// ```
|
2018-04-02 09:11:07 -07:00
|
|
|
///
|
|
|
|
/// (See also the [`std::fs::read`] convenience function for reading from a
|
|
|
|
/// file.)
|
|
|
|
///
|
|
|
|
/// [`std::fs::read`]: ../fs/fn.read.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")]
|
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
|
2015-01-31 20:24:36 -08:00
|
|
|
read_to_end(self, buf)
|
|
|
|
}
|
|
|
|
|
2018-01-05 04:24:12 -08:00
|
|
|
/// Read all bytes until EOF in this source, appending them to `buf`.
|
2015-01-31 20:24:36 -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
|
|
|
/// If successful, this function returns the number of bytes which were read
|
|
|
|
/// and appended to `buf`.
|
|
|
|
///
|
2015-01-31 20:24:36 -08:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If the data in this stream is *not* valid UTF-8 then an error is
|
|
|
|
/// returned and `buf` is unchanged.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// See [`read_to_end`][readtoend] for other error semantics.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// [readtoend]: #method.read_to_end
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
2016-01-13 23:19:24 +00:00
|
|
|
/// [file]: ../fs/struct.File.html
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = String::new();
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// f.read_to_string(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04:00
|
|
|
/// ```
|
2018-04-02 09:11:07 -07:00
|
|
|
///
|
|
|
|
/// (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
|
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")]
|
|
|
|
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
|
2015-01-31 20:24:36 -08:00
|
|
|
// Note that we do *not* call `.read_to_end()` here. We are passing
|
|
|
|
// `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
|
|
|
|
// method to fill it up. An arbitrary implementation could overwrite the
|
|
|
|
// entire contents of the vector, not just append to it (which is what
|
|
|
|
// we are expecting).
|
|
|
|
//
|
|
|
|
// To prevent extraneously checking the UTF-8-ness of the entire buffer
|
|
|
|
// we pass it to our hardcoded `read_to_end` implementation which we
|
|
|
|
// know is guaranteed to only read data into the end of the buffer.
|
|
|
|
append_to_string(buf, |b| read_to_end(self, b))
|
|
|
|
}
|
|
|
|
|
2015-07-20 00:23:37 -03:00
|
|
|
/// Read the exact number of bytes required to fill `buf`.
|
|
|
|
///
|
|
|
|
/// This function reads as many bytes as necessary to completely fill the
|
|
|
|
/// specified buffer `buf`.
|
|
|
|
///
|
|
|
|
/// No guarantees are provided about the contents of `buf` when this
|
|
|
|
/// function is called, implementations cannot rely on any property of the
|
|
|
|
/// contents of `buf` being true. It is recommended that implementations
|
|
|
|
/// only write data to `buf` instead of reading its contents.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If this function encounters an error of the kind
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`ErrorKind::Interrupted`] then the error is ignored and the operation
|
2015-07-20 00:23:37 -03:00
|
|
|
/// will continue.
|
|
|
|
///
|
|
|
|
/// If this function encounters an "end of file" before completely filling
|
2017-08-21 11:45:33 +02:00
|
|
|
/// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
|
2015-07-20 00:23:37 -03:00
|
|
|
/// The contents of `buf` are unspecified in this case.
|
|
|
|
///
|
|
|
|
/// If any other read error is encountered then this function immediately
|
|
|
|
/// returns. The contents of `buf` are unspecified in this case.
|
|
|
|
///
|
|
|
|
/// If this function returns an error, it is unspecified how many bytes it
|
|
|
|
/// has read, but it will never read more than would be necessary to
|
|
|
|
/// completely fill the buffer.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]s implement `Read`:
|
2015-07-20 00:23:37 -03:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]: ../fs/struct.File.html
|
|
|
|
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
|
|
|
|
/// [`ErrorKind::UnexpectedEof`]: ../../std/io/enum.ErrorKind.html#variant.UnexpectedEof
|
2015-07-20 00:23:37 -03:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-20 00:23:37 -03:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = [0; 10];
|
2015-07-20 00:23:37 -03:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read exactly 10 bytes
|
|
|
|
/// f.read_exact(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-20 00:23:37 -03:00
|
|
|
/// ```
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "read_exact", since = "1.6.0")]
|
2015-07-20 00:23:37 -03:00
|
|
|
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match self.read(buf) {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !buf.is_empty() {
|
2015-12-02 17:31:49 -08:00
|
|
|
Err(Error::new(ErrorKind::UnexpectedEof,
|
2015-07-20 00:23:37 -03:00
|
|
|
"failed to fill whole buffer"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Creates a "by reference" adaptor for this instance of `Read`.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// The returned adaptor also implements `Read` and will simply borrow this
|
|
|
|
/// current reader.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
2016-01-13 23:19:24 +00:00
|
|
|
/// [file]: ../fs/struct.File.html
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::Read;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = Vec::new();
|
|
|
|
/// let mut other_buffer = Vec::new();
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// {
|
|
|
|
/// let reference = f.by_ref();
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read at most 5 bytes
|
|
|
|
/// reference.take(5).read_to_end(&mut buffer)?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// } // drop our &mut reference so we can use f again
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // original file still usable, read the rest
|
|
|
|
/// f.read_to_end(&mut other_buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04:00
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 by_ref(&mut self) -> &mut Self where Self: Sized { self }
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2017-08-21 11:45:33 +02:00
|
|
|
/// Transforms this `Read` instance to an [`Iterator`] over its bytes.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-10-31 12:30:15 -07:00
|
|
|
/// The returned type implements [`Iterator`] where the `Item` is
|
2017-11-14 17:22:57 +08:00
|
|
|
/// [`Result`]`<`[`u8`]`, `[`io::Error`]`>`.
|
2017-10-31 12:30:15 -07:00
|
|
|
/// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
|
|
|
|
/// otherwise. EOF is mapped to returning [`None`] from this iterator.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
2016-01-13 23:19:24 +00:00
|
|
|
/// [file]: ../fs/struct.File.html
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`Iterator`]: ../../std/iter/trait.Iterator.html
|
|
|
|
/// [`Result`]: ../../std/result/enum.Result.html
|
2017-11-14 17:22:57 +08:00
|
|
|
/// [`io::Error`]: ../../std/io/struct.Error.html
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`u8`]: ../../std/primitive.u8.html
|
|
|
|
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
|
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// for byte in f.bytes() {
|
|
|
|
/// println!("{}", byte.unwrap());
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
2015-07-09 16:23:27 -04:00
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 bytes(self) -> Bytes<Self> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Bytes { inner: self }
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Creates an adaptor which will chain this stream with another.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// The returned `Read` instance will first read all bytes from this object
|
|
|
|
/// until EOF is encountered. Afterwards the output is equivalent to the
|
|
|
|
/// output of `next`.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
2016-01-13 23:19:24 +00:00
|
|
|
/// [file]: ../fs/struct.File.html
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f1 = File::open("foo.txt")?;
|
|
|
|
/// let mut f2 = File::open("bar.txt")?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let mut handle = f1.chain(f2);
|
|
|
|
/// let mut buffer = String::new();
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // 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(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04:00
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Chain { first: self, second: next, done_first: false }
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Creates an adaptor which will read at most `limit` bytes from it.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// This function returns a new instance of `Read` which will read at most
|
2017-08-21 11:45:33 +02:00
|
|
|
/// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
|
2015-01-31 20:24:36 -08:00
|
|
|
/// read errors will not count towards the number of bytes read and future
|
2017-08-21 11:45:33 +02:00
|
|
|
/// calls to [`read()`] may succeed.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]s implement `Read`:
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2017-08-21 11:45:33 +02:00
|
|
|
/// [`File`]: ../fs/struct.File.html
|
|
|
|
/// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`read()`]: trait.Read.html#tymethod.read
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
|
|
|
/// let mut buffer = [0; 5];
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read at most five bytes
|
|
|
|
/// let mut handle = f.take(5);
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// handle.read(&mut buffer)?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04:00
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 take(self, limit: u64) -> Take<Self> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Take { inner: self, limit: limit }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 21:29:18 -04:00
|
|
|
/// A type used to conditionally initialize buffers passed to `Read` methods.
|
|
|
|
#[unstable(feature = "read_initializer", issue = "42788")]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Initializer(bool);
|
|
|
|
|
|
|
|
impl Initializer {
|
|
|
|
/// Returns a new `Initializer` which will zero out buffers.
|
|
|
|
#[unstable(feature = "read_initializer", issue = "42788")]
|
|
|
|
#[inline]
|
|
|
|
pub fn zeroing() -> Initializer {
|
|
|
|
Initializer(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new `Initializer` which will not zero out buffers.
|
|
|
|
///
|
2017-08-24 17:33:36 +02:00
|
|
|
/// # Safety
|
2017-05-14 21:29:18 -04:00
|
|
|
///
|
|
|
|
/// This may only be called by `Read`ers which guarantee that they will not
|
|
|
|
/// read from buffers passed to `Read` methods, and that the return value of
|
|
|
|
/// the method accurately reflects the number of bytes that have been
|
|
|
|
/// written to the head of the buffer.
|
|
|
|
#[unstable(feature = "read_initializer", issue = "42788")]
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn nop() -> Initializer {
|
|
|
|
Initializer(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if a buffer should be initialized.
|
|
|
|
#[unstable(feature = "read_initializer", issue = "42788")]
|
|
|
|
#[inline]
|
|
|
|
pub fn should_initialize(&self) -> bool {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes a buffer if necessary.
|
|
|
|
#[unstable(feature = "read_initializer", issue = "42788")]
|
|
|
|
#[inline]
|
|
|
|
pub fn initialize(&self, buf: &mut [u8]) {
|
|
|
|
if self.should_initialize() {
|
|
|
|
unsafe { ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
/// A trait for objects which are byte-oriented sinks.
|
|
|
|
///
|
2015-07-09 17:13:32 -04:00
|
|
|
/// Implementors of the `Write` trait are sometimes called 'writers'.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// Writers are defined by two required methods, [`write`] and [`flush`]:
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// * The [`write`] method will attempt to write some data into the object,
|
2015-07-09 16:23:27 -04:00
|
|
|
/// returning how many bytes were successfully written.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// * The [`flush`] method is useful for adaptors and explicit buffers
|
2015-07-09 16:23:27 -04:00
|
|
|
/// themselves for ensuring that all buffered data has been pushed out to the
|
|
|
|
/// 'true sink'.
|
|
|
|
///
|
|
|
|
/// Writers are intended to be composable with one another. Many implementors
|
2016-11-04 20:51:57 +01:00
|
|
|
/// throughout [`std::io`] take and provide types which implement the `Write`
|
2015-07-09 16:23:27 -04:00
|
|
|
/// trait.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`write`]: #tymethod.write
|
|
|
|
/// [`flush`]: #tymethod.flush
|
2016-11-04 20:51:57 +01:00
|
|
|
/// [`std::io`]: index.html
|
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:23:27 -04:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = File::create("foo.txt")?;
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// buffer.write(b"some bytes")?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:23:27 -04: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")]
|
2017-10-04 15:51:35 -05:00
|
|
|
#[doc(spotlight)]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub trait Write {
|
|
|
|
/// Write a buffer into this object, returning how many bytes were written.
|
|
|
|
///
|
|
|
|
/// This function will attempt to write the entire contents of `buf`, but
|
|
|
|
/// the entire write may not succeed, or the write may also generate an
|
|
|
|
/// error. A call to `write` represents *at most one* attempt to write to
|
|
|
|
/// any wrapped object.
|
|
|
|
///
|
|
|
|
/// Calls to `write` are not guaranteed to block waiting for data to be
|
2015-05-25 01:53:44 +02:00
|
|
|
/// written, and a write which would otherwise block can be indicated through
|
2018-01-08 14:16:16 +01:00
|
|
|
/// an [`Err`] variant.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2018-01-08 14:16:16 +01:00
|
|
|
/// If the return value is [`Ok(n)`] then it must be guaranteed that
|
2015-01-31 20:24:36 -08:00
|
|
|
/// `0 <= n <= buf.len()`. A return value of `0` typically means that the
|
|
|
|
/// underlying object is no longer able to accept bytes and will likely not
|
|
|
|
/// be able to in the future as well, or that the buffer provided is empty.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Each call to `write` may generate an I/O error indicating that the
|
|
|
|
/// operation could not be completed. If an error is returned then no bytes
|
|
|
|
/// in the buffer were written to this writer.
|
|
|
|
///
|
|
|
|
/// It is **not** considered an error if the entire buffer could not be
|
|
|
|
/// written to this writer.
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-01-08 14:16:16 +01:00
|
|
|
/// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the
|
2017-04-21 10:32:13 +02:00
|
|
|
/// write operation should be retried if there is nothing else to do.
|
|
|
|
///
|
2018-01-08 14:16:16 +01:00
|
|
|
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
|
|
|
|
/// [`Ok(n)`]: ../../std/result/enum.Result.html#variant.Ok
|
|
|
|
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
|
|
|
|
///
|
2015-07-09 17:13:32 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 17:13:32 -04:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = File::create("foo.txt")?;
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // Writes some prefix of the byte string, not necessarily all of it.
|
|
|
|
/// buffer.write(b"some bytes")?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 17:13:32 -04: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-01-31 20:24:36 -08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize>;
|
|
|
|
|
|
|
|
/// Flush this output stream, ensuring that all intermediately buffered
|
|
|
|
/// contents reach their destination.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// It is considered an error if not all bytes could be written due to
|
|
|
|
/// I/O errors or EOF being reached.
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 17:13:32 -04:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::io::BufWriter;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = BufWriter::new(File::create("foo.txt")?);
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// buffer.write(b"some bytes")?;
|
|
|
|
/// buffer.flush()?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 17:13:32 -04:00
|
|
|
/// ```
|
2015-03-16 10:56:08 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
fn flush(&mut self) -> Result<()>;
|
|
|
|
|
|
|
|
/// Attempts to write an entire buffer into this write.
|
|
|
|
///
|
2018-01-08 14:16:16 +01:00
|
|
|
/// This method will continuously call [`write`] until there is no more data
|
|
|
|
/// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
|
2017-04-21 10:32:13 +02:00
|
|
|
/// returned. This method will not return until the entire buffer has been
|
|
|
|
/// successfully written or such an error occurs. The first error that is
|
2018-01-08 14:16:16 +01:00
|
|
|
/// not of [`ErrorKind::Interrupted`] kind generated from this method will be
|
2017-04-21 10:32:13 +02:00
|
|
|
/// returned.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2017-04-21 10:32:13 +02:00
|
|
|
/// This function will return the first error of
|
2018-01-08 14:16:16 +01:00
|
|
|
/// non-[`ErrorKind::Interrupted`] kind that [`write`] returns.
|
|
|
|
///
|
|
|
|
/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
|
|
|
|
/// [`write`]: #tymethod.write
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 17:13:32 -04:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = File::create("foo.txt")?;
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// buffer.write_all(b"some bytes")?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 17:13:32 -04: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-01-31 20:24:36 -08:00
|
|
|
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
|
2015-03-24 16:54:09 -07:00
|
|
|
while !buf.is_empty() {
|
2015-01-31 20:24:36 -08:00
|
|
|
match self.write(buf) {
|
|
|
|
Ok(0) => return Err(Error::new(ErrorKind::WriteZero,
|
2015-03-31 16:01:03 -07:00
|
|
|
"failed to write whole buffer")),
|
2015-01-31 20:24:36 -08:00
|
|
|
Ok(n) => buf = &buf[n..],
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes a formatted string into this writer, returning any error
|
|
|
|
/// encountered.
|
|
|
|
///
|
2015-07-09 17:13:32 -04:00
|
|
|
/// This method is primarily used to interface with the
|
|
|
|
/// [`format_args!`][formatargs] macro, but it is rare that this should
|
|
|
|
/// explicitly be called. The [`write!`][write] macro should be favored to
|
|
|
|
/// invoke this method instead.
|
|
|
|
///
|
2016-08-16 10:36:39 +12:00
|
|
|
/// [formatargs]: ../macro.format_args.html
|
|
|
|
/// [write]: ../macro.write.html
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 17:13:32 -04:00
|
|
|
/// This function internally uses the [`write_all`][writeall] method on
|
|
|
|
/// this trait and hence will continuously write data so long as no errors
|
|
|
|
/// are received. This also means that partial writes are not indicated in
|
|
|
|
/// this signature.
|
|
|
|
///
|
|
|
|
/// [writeall]: #method.write_all
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return any I/O error reported while formatting.
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 17:13:32 -04:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = File::create("foo.txt")?;
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // this call
|
|
|
|
/// write!(buffer, "{:.*}", 2, 1.234567)?;
|
|
|
|
/// // turns into this:
|
|
|
|
/// buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 17:13:32 -04: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-01-31 20:24:36 -08:00
|
|
|
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()> {
|
2015-02-14 12:56:32 +13:00
|
|
|
// Create a shim which translates a Write to a fmt::Write and saves
|
2015-01-31 20:24:36 -08:00
|
|
|
// off I/O errors. instead of discarding them
|
|
|
|
struct Adaptor<'a, T: ?Sized + 'a> {
|
|
|
|
inner: &'a mut T,
|
|
|
|
error: Result<()>,
|
|
|
|
}
|
|
|
|
|
2015-02-14 12:56:32 +13:00
|
|
|
impl<'a, T: Write + ?Sized> fmt::Write for Adaptor<'a, T> {
|
2015-01-31 20:24:36 -08:00
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
match self.inner.write_all(s.as_bytes()) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(e) => {
|
|
|
|
self.error = Err(e);
|
|
|
|
Err(fmt::Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output = Adaptor { inner: self, error: Ok(()) };
|
|
|
|
match fmt::write(&mut output, fmt) {
|
|
|
|
Ok(()) => Ok(()),
|
2016-02-26 02:53:47 +01:00
|
|
|
Err(..) => {
|
|
|
|
// check if the error came from the underlying `Write` or not
|
|
|
|
if output.error.is_err() {
|
|
|
|
output.error
|
|
|
|
} else {
|
|
|
|
Err(Error::new(ErrorKind::Other, "formatter error"))
|
|
|
|
}
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Creates a "by reference" adaptor for this instance of `Write`.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// The returned adaptor also implements `Write` and will simply borrow this
|
|
|
|
/// current writer.
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 17:13:32 -04:00
|
|
|
/// use std::io::Write;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = File::create("foo.txt")?;
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let reference = buffer.by_ref();
|
2015-07-09 17:13:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // we can use reference just like our original buffer
|
|
|
|
/// reference.write_all(b"some bytes")?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 17:13:32 -04:00
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 by_ref(&mut self) -> &mut Self where Self: Sized { self }
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
2015-07-09 16:34:32 -04:00
|
|
|
/// The `Seek` trait provides a cursor which can be moved within a stream of
|
|
|
|
/// bytes.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// The stream typically has a fixed size, allowing seeking relative to either
|
|
|
|
/// end or the current offset.
|
2015-07-09 16:34:32 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Seek`:
|
|
|
|
///
|
2015-12-26 00:11:20 +01:00
|
|
|
/// [file]: ../fs/struct.File.html
|
2015-07-09 16:34:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 16:34:32 -04:00
|
|
|
/// use std::io;
|
2015-07-09 17:13:32 -04:00
|
|
|
/// use std::io::prelude::*;
|
2015-07-09 16:34:32 -04:00
|
|
|
/// use std::fs::File;
|
|
|
|
/// use std::io::SeekFrom;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut f = File::open("foo.txt")?;
|
2015-07-09 16:34:32 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // move the cursor 42 bytes from the start of the file
|
|
|
|
/// f.seek(SeekFrom::Start(42))?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 16:34:32 -04:00
|
|
|
/// ```
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub trait Seek {
|
2015-07-09 16:34:32 -04:00
|
|
|
/// Seek to an offset, in bytes, in a stream.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 16:34:32 -04:00
|
|
|
/// A seek beyond the end of a stream is allowed, but implementation
|
|
|
|
/// defined.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-08-02 14:15:00 +02:00
|
|
|
/// If the seek operation completed successfully,
|
|
|
|
/// this method returns the new position from the start of the stream.
|
2016-07-30 13:37:52 +02:00
|
|
|
/// That position can be used later with [`SeekFrom::Start`].
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2015-07-09 16:34:32 -04:00
|
|
|
/// Seeking to a negative offset is considered an error.
|
2016-07-30 13:37:52 +02:00
|
|
|
///
|
|
|
|
/// [`SeekFrom::Start`]: enum.SeekFrom.html#variant.Start
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enumeration of possible methods to seek within an I/O object.
|
2016-07-30 13:37:52 +02:00
|
|
|
///
|
|
|
|
/// It is used by the [`Seek`] trait.
|
|
|
|
///
|
|
|
|
/// [`Seek`]: trait.Seek.html
|
2015-01-31 20:24:36 -08:00
|
|
|
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub enum SeekFrom {
|
|
|
|
/// Set the offset to the provided number of bytes.
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-02-19 16:08:36 -08:00
|
|
|
Start(#[stable(feature = "rust1", since = "1.0.0")] u64),
|
2015-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
/// Set the offset to the size of this object plus the specified number of
|
|
|
|
/// bytes.
|
|
|
|
///
|
2015-12-30 19:54:06 +02:00
|
|
|
/// It is possible to seek beyond the end of an object, but it's an error to
|
2015-01-31 20:24:36 -08:00
|
|
|
/// seek before byte 0.
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-02-19 16:08:36 -08:00
|
|
|
End(#[stable(feature = "rust1", since = "1.0.0")] i64),
|
2015-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
/// Set the offset to the current position plus the specified number of
|
|
|
|
/// bytes.
|
|
|
|
///
|
2015-12-30 19:54:06 +02:00
|
|
|
/// It is possible to seek beyond the end of an object, but it's an error to
|
2015-01-31 20:24:36 -08:00
|
|
|
/// seek before byte 0.
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-02-19 16:08:36 -08:00
|
|
|
Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
|
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
|
|
|
-> Result<usize> {
|
|
|
|
let mut read = 0;
|
2015-01-31 20:24:36 -08:00
|
|
|
loop {
|
|
|
|
let (done, used) = {
|
|
|
|
let available = match r.fill_buf() {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
};
|
2015-12-15 00:03:42 +01:00
|
|
|
match memchr::memchr(delim, available) {
|
2015-01-31 20:24:36 -08:00
|
|
|
Some(i) => {
|
2015-12-02 17:31:49 -08:00
|
|
|
buf.extend_from_slice(&available[..i + 1]);
|
2015-01-31 20:24:36 -08:00
|
|
|
(true, i + 1)
|
|
|
|
}
|
|
|
|
None => {
|
2015-12-02 17:31:49 -08:00
|
|
|
buf.extend_from_slice(available);
|
2015-01-31 20:24:36 -08:00
|
|
|
(false, available.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
r.consume(used);
|
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
|
|
|
read += used;
|
2015-01-31 20:24:36 -08:00
|
|
|
if done || used == 0 {
|
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
|
|
|
return Ok(read);
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 14:04:30 -04:00
|
|
|
/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
|
|
|
|
/// to perform extra ways of reading.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// For example, reading line-by-line is inefficient without using a buffer, so
|
|
|
|
/// if you want to read by line, you'll need `BufRead`, which includes a
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`read_line`] method as well as a [`lines`] iterator.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`:
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 14:04:30 -04:00
|
|
|
/// use std::io;
|
2015-07-10 13:49:12 -04:00
|
|
|
/// use std::io::prelude::*;
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// for line in stdin.lock().lines() {
|
|
|
|
/// println!("{}", line.unwrap());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2016-10-18 20:36:44 +02:00
|
|
|
/// If you have something that implements [`Read`], you can use the [`BufReader`
|
|
|
|
/// type][`BufReader`] to turn it into a `BufRead`.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2016-10-18 20:36:44 +02:00
|
|
|
/// For example, [`File`] implements [`Read`], but not `BufRead`.
|
|
|
|
/// [`BufReader`] to the rescue!
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`BufReader`]: struct.BufReader.html
|
|
|
|
/// [`File`]: ../fs/struct.File.html
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`read_line`]: #method.read_line
|
|
|
|
/// [`lines`]: #method.lines
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`Read`]: trait.Read.html
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-10 13:49:12 -04:00
|
|
|
/// use std::io::{self, BufReader};
|
|
|
|
/// use std::io::prelude::*;
|
2015-07-09 14:04:30 -04:00
|
|
|
/// use std::fs::File;
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let f = File::open("foo.txt")?;
|
|
|
|
/// let f = BufReader::new(f);
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// for line in f.lines() {
|
|
|
|
/// println!("{}", line.unwrap());
|
|
|
|
/// }
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-07-09 14:04:30 -04:00
|
|
|
/// ```
|
2015-05-19 09:49:04 -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-01-31 20:24:36 -08:00
|
|
|
pub trait BufRead: Read {
|
|
|
|
/// Fills the internal buffer of this object, returning the buffer contents.
|
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// This function is a lower-level call. It needs to be paired with the
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`consume`] method to function properly. When calling this
|
2015-07-09 14:04:30 -04:00
|
|
|
/// method, none of the contents will be "read" in the sense that later
|
2017-03-12 14:04:52 -04:00
|
|
|
/// calling `read` may return the same contents. As such, [`consume`] must
|
2016-10-18 20:36:44 +02:00
|
|
|
/// be called with the number of bytes that are consumed from this buffer to
|
2015-07-09 14:04:30 -04:00
|
|
|
/// ensure that the bytes are never returned twice.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`consume`]: #tymethod.consume
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// An empty buffer returned indicates that the stream has reached EOF.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an I/O error if the underlying reader was
|
|
|
|
/// read, but returned an error.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`:
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2015-07-09 14:04:30 -04:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut stdin = stdin.lock();
|
|
|
|
///
|
|
|
|
/// // we can't have two `&mut` references to `stdin`, so use a block
|
|
|
|
/// // to end the borrow early.
|
|
|
|
/// let length = {
|
|
|
|
/// let buffer = stdin.fill_buf().unwrap();
|
|
|
|
///
|
|
|
|
/// // work with buffer
|
|
|
|
/// println!("{:?}", buffer);
|
|
|
|
///
|
|
|
|
/// buffer.len()
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// // ensure the bytes we worked with aren't returned again later
|
|
|
|
/// stdin.consume(length);
|
|
|
|
/// ```
|
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-01-31 20:24:36 -08:00
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]>;
|
|
|
|
|
|
|
|
/// Tells this buffer that `amt` bytes have been consumed from the buffer,
|
|
|
|
/// so they should no longer be returned in calls to `read`.
|
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
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// This function is a lower-level call. It needs to be paired with the
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`fill_buf`] method to function properly. This function does
|
2015-07-09 14:04:30 -04:00
|
|
|
/// not perform any I/O, it simply informs this object that some amount of
|
2017-03-12 14:04:52 -04:00
|
|
|
/// its buffer, returned from [`fill_buf`], has been consumed and should
|
2016-10-18 20:36:44 +02:00
|
|
|
/// no longer be returned. As such, this function may do odd things if
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`fill_buf`] isn't called before calling it.
|
2015-03-22 18:38:40 -04:00
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// The `amt` must be `<=` the number of bytes in the buffer returned by
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`fill_buf`].
|
2015-03-22 18:38:40 -04:00
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// # Examples
|
2015-03-22 18:38:40 -04:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// Since `consume()` is meant to be used with [`fill_buf`],
|
2015-07-09 14:04:30 -04:00
|
|
|
/// that method's example includes an example of `consume()`.
|
2016-10-18 20:36:44 +02:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`fill_buf`]: #tymethod.fill_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
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
fn consume(&mut self, amt: usize);
|
|
|
|
|
2016-09-30 00:00:00 +02:00
|
|
|
/// Read all bytes into `buf` until the delimiter `byte` or EOF is reached.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// This function will read bytes from the underlying stream until the
|
|
|
|
/// delimiter or EOF is found. Once found, all bytes up to, and including,
|
|
|
|
/// the delimiter (if found) will be appended to `buf`.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2016-09-30 00:00:00 +02:00
|
|
|
/// If successful, this function will return the total number of bytes read.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2016-10-18 20:36:44 +02:00
|
|
|
/// This function will ignore all instances of [`ErrorKind::Interrupted`] and
|
2017-03-12 14:04:52 -04:00
|
|
|
/// will otherwise return any errors returned by [`fill_buf`].
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// If an I/O error is encountered then all bytes read so far will be
|
|
|
|
/// present in `buf` and its length will have been adjusted appropriately.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`fill_buf`]: #tymethod.fill_buf
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
|
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// # Examples
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
|
|
|
|
/// this example, we use [`Cursor`] to read all the bytes in a byte slice
|
|
|
|
/// in hyphen delimited segments:
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// [`Cursor`]: struct.Cursor.html
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, BufRead};
|
|
|
|
///
|
|
|
|
/// let mut cursor = io::Cursor::new(b"lorem-ipsum");
|
|
|
|
/// let mut buf = vec![];
|
|
|
|
///
|
|
|
|
/// // cursor is at 'l'
|
|
|
|
/// let num_bytes = cursor.read_until(b'-', &mut buf)
|
|
|
|
/// .expect("reading from cursor won't fail");
|
|
|
|
/// assert_eq!(num_bytes, 6);
|
|
|
|
/// assert_eq!(buf, b"lorem-");
|
|
|
|
/// buf.clear();
|
|
|
|
///
|
|
|
|
/// // cursor is at 'i'
|
|
|
|
/// let num_bytes = cursor.read_until(b'-', &mut buf)
|
|
|
|
/// .expect("reading from cursor won't fail");
|
|
|
|
/// assert_eq!(num_bytes, 5);
|
|
|
|
/// assert_eq!(buf, b"ipsum");
|
|
|
|
/// buf.clear();
|
|
|
|
///
|
|
|
|
/// // cursor is at EOF
|
|
|
|
/// let num_bytes = cursor.read_until(b'-', &mut buf)
|
|
|
|
/// .expect("reading from cursor won't fail");
|
|
|
|
/// assert_eq!(num_bytes, 0);
|
|
|
|
/// assert_eq!(buf, b"");
|
2015-07-09 14:04:30 -04: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")]
|
|
|
|
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
|
2015-01-31 20:24:36 -08:00
|
|
|
read_until(self, byte, buf)
|
|
|
|
}
|
|
|
|
|
2015-07-09 14:04:30 -04:00
|
|
|
/// Read all bytes until a newline (the 0xA byte) is reached, and append
|
|
|
|
/// them to the provided buffer.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// This function will read bytes from the underlying stream until the
|
|
|
|
/// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
|
|
|
|
/// up to, and including, the delimiter (if found) will be appended to
|
|
|
|
/// `buf`.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2016-09-30 00:00:00 +02:00
|
|
|
/// If successful, this function will return the total number of bytes read.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-10-31 11:04:08 +01:00
|
|
|
/// An empty buffer returned indicates that the stream has reached EOF.
|
|
|
|
///
|
2015-01-31 20:24:36 -08:00
|
|
|
/// # Errors
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This function has the same error semantics as [`read_until`] and will
|
2016-10-18 20:36:44 +02:00
|
|
|
/// also return an error if the read bytes are not valid UTF-8. If an I/O
|
|
|
|
/// error is encountered then `buf` may contain some bytes already read in
|
|
|
|
/// the event that all data read so far was valid UTF-8.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2018-02-19 17:19:30 +01:00
|
|
|
/// [`read_until`]: #method.read_until
|
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
|
|
|
|
/// this example, we use [`Cursor`] to read all the lines in a byte slice:
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// [`Cursor`]: struct.Cursor.html
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// ```
|
2017-03-18 10:37:52 -04:00
|
|
|
/// use std::io::{self, BufRead};
|
|
|
|
///
|
|
|
|
/// let mut cursor = io::Cursor::new(b"foo\nbar");
|
|
|
|
/// let mut buf = String::new();
|
|
|
|
///
|
|
|
|
/// // cursor is at 'f'
|
|
|
|
/// let num_bytes = cursor.read_line(&mut buf)
|
|
|
|
/// .expect("reading from cursor won't fail");
|
|
|
|
/// assert_eq!(num_bytes, 4);
|
|
|
|
/// assert_eq!(buf, "foo\n");
|
|
|
|
/// buf.clear();
|
|
|
|
///
|
|
|
|
/// // cursor is at 'b'
|
|
|
|
/// let num_bytes = cursor.read_line(&mut buf)
|
|
|
|
/// .expect("reading from cursor won't fail");
|
|
|
|
/// assert_eq!(num_bytes, 3);
|
|
|
|
/// assert_eq!(buf, "bar");
|
|
|
|
/// buf.clear();
|
|
|
|
///
|
|
|
|
/// // cursor is at EOF
|
|
|
|
/// let num_bytes = cursor.read_line(&mut buf)
|
|
|
|
/// .expect("reading from cursor won't fail");
|
|
|
|
/// assert_eq!(num_bytes, 0);
|
|
|
|
/// assert_eq!(buf, "");
|
2015-07-09 14:04:30 -04: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")]
|
|
|
|
fn read_line(&mut self, buf: &mut String) -> Result<usize> {
|
2015-01-31 20:24:36 -08:00
|
|
|
// Note that we are not calling the `.read_until` method here, but
|
|
|
|
// rather our hardcoded implementation. For more details as to why, see
|
|
|
|
// the comments in `read_to_end`.
|
|
|
|
append_to_string(buf, |b| read_until(self, b'\n', b))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the contents of this reader split on the byte
|
|
|
|
/// `byte`.
|
|
|
|
///
|
|
|
|
/// The iterator returned from this function will return instances of
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`io::Result`]`<`[`Vec<u8>`]`>`. Each vector returned will *not* have
|
|
|
|
/// the delimiter byte at the end.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This function will yield errors whenever [`read_until`] would have
|
2016-10-18 20:36:44 +02:00
|
|
|
/// also yielded an error.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`io::Result`]: type.Result.html
|
|
|
|
/// [`Vec<u8>`]: ../vec/struct.Vec.html
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`read_until`]: #method.read_until
|
2016-10-18 20:36:44 +02:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
|
|
|
|
/// this example, we use [`Cursor`] to iterate over all hyphen delimited
|
|
|
|
/// segments in a byte slice
|
|
|
|
///
|
|
|
|
/// [`Cursor`]: struct.Cursor.html
|
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// ```
|
2017-03-18 10:37:52 -04:00
|
|
|
/// use std::io::{self, BufRead};
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// let cursor = io::Cursor::new(b"lorem-ipsum-dolor");
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// let mut split_iter = cursor.split(b'-').map(|l| l.unwrap());
|
|
|
|
/// assert_eq!(split_iter.next(), Some(b"lorem".to_vec()));
|
|
|
|
/// assert_eq!(split_iter.next(), Some(b"ipsum".to_vec()));
|
|
|
|
/// assert_eq!(split_iter.next(), Some(b"dolor".to_vec()));
|
|
|
|
/// assert_eq!(split_iter.next(), None);
|
2015-07-09 14:04:30 -04:00
|
|
|
/// ```
|
2015-03-26 16:54:15 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 split(self, byte: u8) -> Split<Self> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Split { buf: self, delim: byte }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the lines of this reader.
|
|
|
|
///
|
|
|
|
/// The iterator returned from this function will yield instances of
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`io::Result`]`<`[`String`]`>`. Each string returned will *not* have a newline
|
2015-08-26 17:30:45 -07:00
|
|
|
/// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2016-10-18 20:36:44 +02:00
|
|
|
/// [`io::Result`]: type.Result.html
|
|
|
|
/// [`String`]: ../string/struct.String.html
|
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
|
|
|
|
/// this example, we use [`Cursor`] to iterate over all the lines in a byte
|
|
|
|
/// slice.
|
|
|
|
///
|
|
|
|
/// [`Cursor`]: struct.Cursor.html
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// ```
|
2017-03-18 10:37:52 -04:00
|
|
|
/// use std::io::{self, BufRead};
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
2017-03-18 10:37:52 -04:00
|
|
|
/// let mut lines_iter = cursor.lines().map(|l| l.unwrap());
|
|
|
|
/// assert_eq!(lines_iter.next(), Some(String::from("lorem")));
|
|
|
|
/// assert_eq!(lines_iter.next(), Some(String::from("ipsum")));
|
|
|
|
/// assert_eq!(lines_iter.next(), Some(String::from("dolor")));
|
|
|
|
/// assert_eq!(lines_iter.next(), None);
|
2015-07-09 14:04:30 -04:00
|
|
|
/// ```
|
2016-12-20 17:09:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// Each line of the iterator has the same error semantics as [`BufRead::read_line`].
|
2016-12-20 17:09:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`BufRead::read_line`]: trait.BufRead.html#method.read_line
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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 lines(self) -> Lines<Self> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Lines { buf: self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:16:57 -04:00
|
|
|
/// Adaptor to chain together two readers.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This struct is generally created by calling [`chain`] on a reader.
|
|
|
|
/// Please see the documentation of [`chain`] for more details.
|
2015-07-20 13:16:57 -04:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`chain`]: trait.Read.html#method.chain
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Chain<T, U> {
|
|
|
|
first: T,
|
|
|
|
second: U,
|
|
|
|
done_first: bool,
|
|
|
|
}
|
|
|
|
|
2017-04-22 01:14:18 -07:00
|
|
|
impl<T, U> Chain<T, U> {
|
|
|
|
/// Consumes the `Chain`, returning the wrapped readers.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
2017-04-22 01:14:18 -07:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut foo_file = File::open("foo.txt")?;
|
|
|
|
/// let mut bar_file = File::open("bar.txt")?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let chain = foo_file.chain(bar_file);
|
|
|
|
/// let (foo_file, bar_file) = chain.into_inner();
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-04-22 01:14:18 -07:00
|
|
|
/// ```
|
2017-06-30 18:05:04 -07:00
|
|
|
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
|
2017-04-22 01:14:18 -07:00
|
|
|
pub fn into_inner(self) -> (T, U) {
|
|
|
|
(self.first, self.second)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets references to the underlying readers in this `Chain`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
2017-04-22 01:14:18 -07:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut foo_file = File::open("foo.txt")?;
|
|
|
|
/// let mut bar_file = File::open("bar.txt")?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let chain = foo_file.chain(bar_file);
|
|
|
|
/// let (foo_file, bar_file) = chain.get_ref();
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-04-22 01:14:18 -07:00
|
|
|
/// ```
|
2017-06-30 18:05:04 -07:00
|
|
|
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
|
2017-04-22 01:14:18 -07:00
|
|
|
pub fn get_ref(&self) -> (&T, &U) {
|
|
|
|
(&self.first, &self.second)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets mutable references to the underlying readers in this `Chain`.
|
|
|
|
///
|
2017-04-24 16:46:21 -07:00
|
|
|
/// Care should be taken to avoid modifying the internal I/O state of the
|
|
|
|
/// underlying readers as doing so may corrupt the internal state of this
|
|
|
|
/// `Chain`.
|
|
|
|
///
|
2017-04-22 01:14:18 -07:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
2017-04-22 01:14:18 -07:00
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut foo_file = File::open("foo.txt")?;
|
|
|
|
/// let mut bar_file = File::open("bar.txt")?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let mut chain = foo_file.chain(bar_file);
|
|
|
|
/// let (foo_file, bar_file) = chain.get_mut();
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-04-22 01:14:18 -07:00
|
|
|
/// ```
|
2017-06-30 18:05:04 -07:00
|
|
|
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
|
2017-04-22 01:14:18 -07:00
|
|
|
pub fn get_mut(&mut self) -> (&mut T, &mut U) {
|
|
|
|
(&mut self.first, &mut self.second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("Chain")
|
|
|
|
.field("t", &self.first)
|
|
|
|
.field("u", &self.second)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<T: Read, U: Read> Read for Chain<T, U> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
if !self.done_first {
|
2016-03-22 22:01:37 -05:00
|
|
|
match self.first.read(buf)? {
|
2016-09-27 00:00:00 +02:00
|
|
|
0 if buf.len() != 0 => { self.done_first = true; }
|
2015-01-31 20:24:36 -08:00
|
|
|
n => return Ok(n),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.second.read(buf)
|
|
|
|
}
|
2017-05-14 21:29:18 -04:00
|
|
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
let initializer = self.first.initializer();
|
|
|
|
if initializer.should_initialize() {
|
|
|
|
initializer
|
|
|
|
} else {
|
|
|
|
self.second.initializer()
|
|
|
|
}
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
2016-03-28 01:28:03 +02:00
|
|
|
#[stable(feature = "chain_bufread", since = "1.9.0")]
|
|
|
|
impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]> {
|
|
|
|
if !self.done_first {
|
2016-03-28 21:37:36 +02:00
|
|
|
match self.first.fill_buf()? {
|
2016-03-28 01:28:03 +02:00
|
|
|
buf if buf.len() == 0 => { self.done_first = true; }
|
|
|
|
buf => return Ok(buf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.second.fill_buf()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
|
|
|
if !self.done_first {
|
|
|
|
self.first.consume(amt)
|
|
|
|
} else {
|
|
|
|
self.second.consume(amt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
/// Reader adaptor which limits the bytes read from an underlying reader.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This struct is generally created by calling [`take`] on a reader.
|
|
|
|
/// Please see the documentation of [`take`] for more details.
|
2015-07-20 15:52:43 -04:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`take`]: trait.Read.html#method.take
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
#[derive(Debug)]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Take<T> {
|
|
|
|
inner: T,
|
|
|
|
limit: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Take<T> {
|
|
|
|
/// Returns the number of bytes that can be read before this instance will
|
|
|
|
/// return EOF.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
2016-11-04 20:51:57 +01:00
|
|
|
/// This instance may reach `EOF` after reading fewer bytes than indicated by
|
|
|
|
/// this method if the underlying [`Read`] instance reaches EOF.
|
|
|
|
///
|
|
|
|
/// [`Read`]: ../../std/io/trait.Read.html
|
2016-07-30 13:30:41 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2016-07-30 13:30:41 +02:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let f = File::open("foo.txt")?;
|
2016-07-30 13:30:41 +02:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read at most five bytes
|
|
|
|
/// let handle = f.take(5);
|
2016-07-30 13:30:41 +02:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// println!("limit: {}", handle.limit());
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2016-07-30 13:30:41 +02:00
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub fn limit(&self) -> u64 { self.limit }
|
2016-08-26 10:59:46 -07:00
|
|
|
|
2017-06-16 08:24:10 -06:00
|
|
|
/// Sets the number of bytes that can be read before this instance will
|
|
|
|
/// return EOF. This is the same as constructing a new `Take` instance, so
|
|
|
|
/// the amount of bytes read and the previous limit value don't matter when
|
|
|
|
/// calling this method.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2017-06-16 08:24:10 -06:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let f = File::open("foo.txt")?;
|
2017-06-16 08:24:10 -06:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// // read at most five bytes
|
|
|
|
/// let mut handle = f.take(5);
|
|
|
|
/// handle.set_limit(10);
|
2017-06-16 08:24:10 -06:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// assert_eq!(handle.limit(), 10);
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-06-16 08:24:10 -06:00
|
|
|
/// ```
|
2018-04-05 00:30:49 -06:00
|
|
|
#[stable(feature = "take_set_limit", since = "1.27.0")]
|
2017-06-16 08:24:10 -06:00
|
|
|
pub fn set_limit(&mut self, limit: u64) {
|
|
|
|
self.limit = limit;
|
|
|
|
}
|
|
|
|
|
2016-08-26 10:59:46 -07:00
|
|
|
/// Consumes the `Take`, returning the wrapped reader.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2016-08-26 10:59:46 -07:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut file = File::open("foo.txt")?;
|
2016-08-26 10:59:46 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let mut buffer = [0; 5];
|
|
|
|
/// let mut handle = file.take(5);
|
|
|
|
/// handle.read(&mut buffer)?;
|
2016-08-26 10:59:46 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let file = handle.into_inner();
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2016-08-26 10:59:46 -07:00
|
|
|
/// ```
|
2016-12-14 12:39:16 -08:00
|
|
|
#[stable(feature = "io_take_into_inner", since = "1.15.0")]
|
2016-08-26 10:59:46 -07:00
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.inner
|
|
|
|
}
|
2017-04-22 01:14:18 -07:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying reader.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2017-04-22 01:14:18 -07:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut file = File::open("foo.txt")?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let mut buffer = [0; 5];
|
|
|
|
/// let mut handle = file.take(5);
|
|
|
|
/// handle.read(&mut buffer)?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let file = handle.get_ref();
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-04-22 01:14:18 -07:00
|
|
|
/// ```
|
2017-06-30 18:05:04 -07:00
|
|
|
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
|
2017-04-22 01:14:18 -07:00
|
|
|
pub fn get_ref(&self) -> &T {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a mutable reference to the underlying reader.
|
|
|
|
///
|
2017-04-24 16:46:21 -07:00
|
|
|
/// Care should be taken to avoid modifying the internal I/O state of the
|
|
|
|
/// underlying reader as doing so may corrupt the internal limit of this
|
|
|
|
/// `Take`.
|
|
|
|
///
|
2017-04-22 01:14:18 -07:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// ```no_run
|
2017-04-22 01:14:18 -07:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut file = File::open("foo.txt")?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let mut buffer = [0; 5];
|
|
|
|
/// let mut handle = file.take(5);
|
|
|
|
/// handle.read(&mut buffer)?;
|
2017-04-22 01:14:18 -07:00
|
|
|
///
|
2018-03-24 22:56:07 -04:00
|
|
|
/// let file = handle.get_mut();
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-04-22 01:14:18 -07:00
|
|
|
/// ```
|
2017-06-30 18:05:04 -07:00
|
|
|
#[stable(feature = "more_io_inner_methods", since = "1.20.0")]
|
2017-04-22 01:14:18 -07:00
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<T: Read> Read for Take<T> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
2015-02-21 11:15:58 -08:00
|
|
|
// Don't call into inner reader at all at EOF because it may still block
|
|
|
|
if self.limit == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
let max = cmp::min(buf.len() as u64, self.limit) as usize;
|
2016-03-22 22:01:37 -05:00
|
|
|
let n = self.inner.read(&mut buf[..max])?;
|
2015-01-31 20:24:36 -08:00
|
|
|
self.limit -= n as u64;
|
|
|
|
Ok(n)
|
|
|
|
}
|
2017-05-14 21:29:18 -04:00
|
|
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
self.inner.initializer()
|
|
|
|
}
|
2018-08-01 10:23:07 +02:00
|
|
|
|
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
|
|
|
|
let reservation_size = cmp::min(self.limit, 32) as usize;
|
|
|
|
|
|
|
|
read_to_end_with_reservation(self, buf, reservation_size)
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-21 14:59:29 -08:00
|
|
|
impl<T: BufRead> BufRead for Take<T> {
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]> {
|
2016-04-09 14:23:11 +02:00
|
|
|
// Don't call into inner reader at all at EOF because it may still block
|
|
|
|
if self.limit == 0 {
|
|
|
|
return Ok(&[]);
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
let buf = self.inner.fill_buf()?;
|
2015-02-21 14:59:29 -08:00
|
|
|
let cap = cmp::min(buf.len() as u64, self.limit) as usize;
|
|
|
|
Ok(&buf[..cap])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
|
|
|
// Don't let callers reset the limit by passing an overlarge value
|
|
|
|
let amt = cmp::min(amt as u64, self.limit) as usize;
|
|
|
|
self.limit -= amt as u64;
|
|
|
|
self.inner.consume(amt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 20:35:36 +02:00
|
|
|
fn read_one_byte(reader: &mut dyn Read) -> Option<Result<u8>> {
|
2016-05-30 19:10:16 -07:00
|
|
|
let mut buf = [0];
|
|
|
|
loop {
|
|
|
|
return match reader.read(&mut buf) {
|
|
|
|
Ok(0) => None,
|
|
|
|
Ok(..) => Some(Ok(buf[0])),
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
|
|
|
Err(e) => Some(Err(e)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:16:57 -04:00
|
|
|
/// An iterator over `u8` values of a reader.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This struct is generally created by calling [`bytes`] on a reader.
|
|
|
|
/// Please see the documentation of [`bytes`] for more details.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`bytes`]: trait.Read.html#method.bytes
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
#[derive(Debug)]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Bytes<R> {
|
|
|
|
inner: R,
|
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<R: Read> Iterator for Bytes<R> {
|
|
|
|
type Item = Result<u8>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<u8>> {
|
2016-05-30 19:10:16 -07:00
|
|
|
read_one_byte(&mut self.inner)
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over the contents of an instance of `BufRead` split on a
|
|
|
|
/// particular byte.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This struct is generally created by calling [`split`][split] on a
|
2015-07-20 13:16:57 -04:00
|
|
|
/// `BufRead`. Please see the documentation of `split()` for more details.
|
|
|
|
///
|
|
|
|
/// [split]: trait.BufRead.html#method.split
|
2015-03-26 16:54:15 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
#[derive(Debug)]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Split<B> {
|
|
|
|
buf: B,
|
|
|
|
delim: u8,
|
|
|
|
}
|
|
|
|
|
2015-03-26 16:54:15 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<B: BufRead> Iterator for Split<B> {
|
|
|
|
type Item = Result<Vec<u8>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<Vec<u8>>> {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
match self.buf.read_until(self.delim, &mut 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
|
|
|
Ok(0) => None,
|
|
|
|
Ok(_n) => {
|
2015-01-31 20:24:36 -08:00
|
|
|
if buf[buf.len() - 1] == self.delim {
|
|
|
|
buf.pop();
|
|
|
|
}
|
|
|
|
Some(Ok(buf))
|
|
|
|
}
|
|
|
|
Err(e) => Some(Err(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:16:57 -04:00
|
|
|
/// An iterator over the lines of an instance of `BufRead`.
|
|
|
|
///
|
2017-03-12 14:04:52 -04:00
|
|
|
/// This struct is generally created by calling [`lines`][lines] on a
|
2015-07-20 13:16:57 -04:00
|
|
|
/// `BufRead`. Please see the documentation of `lines()` for more details.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-20 13:16:57 -04:00
|
|
|
/// [lines]: trait.BufRead.html#method.lines
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
#[derive(Debug)]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Lines<B> {
|
|
|
|
buf: B,
|
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 14:49:03 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<B: BufRead> Iterator for Lines<B> {
|
|
|
|
type Item = Result<String>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<String>> {
|
|
|
|
let mut buf = String::new();
|
|
|
|
match self.buf.read_line(&mut 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
|
|
|
Ok(0) => None,
|
|
|
|
Ok(_n) => {
|
2015-01-31 20:24:36 -08:00
|
|
|
if buf.ends_with("\n") {
|
|
|
|
buf.pop();
|
2015-08-26 17:30:45 -07:00
|
|
|
if buf.ends_with("\r") {
|
|
|
|
buf.pop();
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
Some(Ok(buf))
|
|
|
|
}
|
|
|
|
Err(e) => Some(Err(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use io::prelude::*;
|
2015-02-21 11:15:58 -08:00
|
|
|
use io;
|
2015-01-31 20:24:36 -08:00
|
|
|
use super::Cursor;
|
2015-07-10 17:34:07 +01:00
|
|
|
use test;
|
|
|
|
use super::repeat;
|
2015-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
#[test]
|
2016-09-07 05:34:15 +00:00
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
2015-01-31 20:24:36 -08:00
|
|
|
fn read_until() {
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut buf = Cursor::new(&b"12"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = Vec::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, b"12");
|
|
|
|
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut buf = Cursor::new(&b"1233"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = Vec::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, b"123");
|
|
|
|
v.truncate(0);
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, b"3");
|
|
|
|
v.truncate(0);
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split() {
|
2015-03-18 09:16:08 +03:00
|
|
|
let buf = Cursor::new(&b"12"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut s = buf.split(b'3');
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
|
|
|
|
assert!(s.next().is_none());
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2015-03-18 09:16:08 +03:00
|
|
|
let buf = Cursor::new(&b"1233"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut s = buf.split(b'3');
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
|
|
|
|
assert_eq!(s.next().unwrap().unwrap(), vec![]);
|
|
|
|
assert!(s.next().is_none());
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_line() {
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut buf = Cursor::new(&b"12"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = String::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_line(&mut v).unwrap(), 2);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, "12");
|
|
|
|
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut buf = Cursor::new(&b"12\n\n"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = String::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_line(&mut v).unwrap(), 3);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, "12\n");
|
|
|
|
v.truncate(0);
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_line(&mut v).unwrap(), 1);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, "\n");
|
|
|
|
v.truncate(0);
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(buf.read_line(&mut v).unwrap(), 0);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lines() {
|
2015-08-26 17:30:45 -07:00
|
|
|
let buf = Cursor::new(&b"12\r"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut s = buf.lines();
|
2015-08-26 17:30:45 -07:00
|
|
|
assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
|
2015-03-31 16:20:09 -07:00
|
|
|
assert!(s.next().is_none());
|
2015-01-31 20:24:36 -08:00
|
|
|
|
2015-08-26 17:30:45 -07:00
|
|
|
let buf = Cursor::new(&b"12\r\n\n"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut s = buf.lines();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
|
|
|
|
assert_eq!(s.next().unwrap().unwrap(), "".to_string());
|
|
|
|
assert!(s.next().is_none());
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_to_end() {
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut c = Cursor::new(&b""[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = Vec::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, []);
|
|
|
|
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut c = Cursor::new(&b"1"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = Vec::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, b"1");
|
2015-07-07 13:50:23 +02:00
|
|
|
|
|
|
|
let cap = 1024 * 1024;
|
|
|
|
let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
|
|
|
|
let mut v = Vec::new();
|
|
|
|
let (a, b) = data.split_at(data.len() / 2);
|
|
|
|
assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
|
|
|
|
assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
|
|
|
|
assert_eq!(v, data);
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_to_string() {
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut c = Cursor::new(&b""[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = String::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, "");
|
|
|
|
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut c = Cursor::new(&b"1"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = String::new();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
|
2015-01-31 20:24:36 -08:00
|
|
|
assert_eq!(v, "1");
|
|
|
|
|
2015-03-18 09:16:08 +03:00
|
|
|
let mut c = Cursor::new(&b"\xff"[..]);
|
2015-01-31 20:24:36 -08:00
|
|
|
let mut v = String::new();
|
|
|
|
assert!(c.read_to_string(&mut v).is_err());
|
|
|
|
}
|
2015-02-21 11:15:58 -08:00
|
|
|
|
2015-07-20 00:23:37 -03:00
|
|
|
#[test]
|
|
|
|
fn read_exact() {
|
|
|
|
let mut buf = [0; 4];
|
|
|
|
|
|
|
|
let mut c = Cursor::new(&b""[..]);
|
|
|
|
assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
|
2015-12-02 17:31:49 -08:00
|
|
|
io::ErrorKind::UnexpectedEof);
|
2015-07-20 00:23:37 -03:00
|
|
|
|
|
|
|
let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
|
|
|
|
c.read_exact(&mut buf).unwrap();
|
|
|
|
assert_eq!(&buf, b"1234");
|
|
|
|
c.read_exact(&mut buf).unwrap();
|
|
|
|
assert_eq!(&buf, b"5678");
|
|
|
|
assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
|
2015-12-02 17:31:49 -08:00
|
|
|
io::ErrorKind::UnexpectedEof);
|
2015-07-20 00:23:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_exact_slice() {
|
|
|
|
let mut buf = [0; 4];
|
|
|
|
|
|
|
|
let mut c = &b""[..];
|
|
|
|
assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
|
2015-12-02 17:31:49 -08:00
|
|
|
io::ErrorKind::UnexpectedEof);
|
2015-07-20 00:23:37 -03:00
|
|
|
|
|
|
|
let mut c = &b"123"[..];
|
|
|
|
assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
|
2015-12-02 17:31:49 -08:00
|
|
|
io::ErrorKind::UnexpectedEof);
|
2015-07-20 00:23:37 -03:00
|
|
|
// make sure the optimized (early returning) method is being used
|
|
|
|
assert_eq!(&buf, &[0; 4]);
|
|
|
|
|
|
|
|
let mut c = &b"1234"[..];
|
|
|
|
c.read_exact(&mut buf).unwrap();
|
|
|
|
assert_eq!(&buf, b"1234");
|
|
|
|
|
|
|
|
let mut c = &b"56789"[..];
|
|
|
|
c.read_exact(&mut buf).unwrap();
|
|
|
|
assert_eq!(&buf, b"5678");
|
|
|
|
assert_eq!(c, b"9");
|
|
|
|
}
|
|
|
|
|
2015-02-21 11:15:58 -08:00
|
|
|
#[test]
|
|
|
|
fn take_eof() {
|
|
|
|
struct R;
|
|
|
|
|
|
|
|
impl Read for R {
|
2015-02-28 20:07:05 +02:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
|
2015-03-31 16:20:09 -07:00
|
|
|
Err(io::Error::new(io::ErrorKind::Other, ""))
|
2015-02-21 11:15:58 -08:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 14:23:11 +02:00
|
|
|
impl BufRead for R {
|
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, ""))
|
|
|
|
}
|
|
|
|
fn consume(&mut self, _amt: usize) { }
|
|
|
|
}
|
2015-02-21 11:15:58 -08:00
|
|
|
|
|
|
|
let mut buf = [0; 1];
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(0, R.take(0).read(&mut buf).unwrap());
|
2016-04-09 14:23:11 +02:00
|
|
|
assert_eq!(b"", R.take(0).fill_buf().unwrap());
|
2015-02-21 11:15:58 -08:00
|
|
|
}
|
2015-07-10 17:34:07 +01:00
|
|
|
|
2016-03-28 21:37:36 +02:00
|
|
|
fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
|
|
|
|
let mut cat = Vec::new();
|
|
|
|
loop {
|
|
|
|
let consume = {
|
|
|
|
let buf1 = br1.fill_buf().unwrap();
|
|
|
|
let buf2 = br2.fill_buf().unwrap();
|
|
|
|
let minlen = if buf1.len() < buf2.len() { buf1.len() } else { buf2.len() };
|
|
|
|
assert_eq!(buf1[..minlen], buf2[..minlen]);
|
|
|
|
cat.extend_from_slice(&buf1[..minlen]);
|
|
|
|
minlen
|
|
|
|
};
|
|
|
|
if consume == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
br1.consume(consume);
|
|
|
|
br2.consume(consume);
|
|
|
|
}
|
|
|
|
assert_eq!(br1.fill_buf().unwrap().len(), 0);
|
|
|
|
assert_eq!(br2.fill_buf().unwrap().len(), 0);
|
|
|
|
assert_eq!(&cat[..], &exp[..])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chain_bufread() {
|
|
|
|
let testdata = b"ABCDEFGHIJKL";
|
2016-03-28 22:40:46 +02:00
|
|
|
let chain1 = (&testdata[..3]).chain(&testdata[3..6])
|
|
|
|
.chain(&testdata[6..9])
|
|
|
|
.chain(&testdata[9..]);
|
|
|
|
let chain2 = (&testdata[..4]).chain(&testdata[4..8])
|
|
|
|
.chain(&testdata[8..]);
|
2016-03-28 21:37:36 +02:00
|
|
|
cmp_bufread(chain1, chain2, &testdata[..]);
|
|
|
|
}
|
|
|
|
|
2016-09-27 00:00:00 +02:00
|
|
|
#[test]
|
|
|
|
fn chain_zero_length_read_is_not_eof() {
|
|
|
|
let a = b"A";
|
|
|
|
let b = b"B";
|
|
|
|
let mut s = String::new();
|
|
|
|
let mut chain = (&a[..]).chain(&b[..]);
|
|
|
|
chain.read(&mut []).unwrap();
|
|
|
|
chain.read_to_string(&mut s).unwrap();
|
|
|
|
assert_eq!("AB", s);
|
|
|
|
}
|
|
|
|
|
2015-07-10 17:34:07 +01:00
|
|
|
#[bench]
|
2016-09-07 05:34:15 +00:00
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
2015-07-10 17:34:07 +01:00
|
|
|
fn bench_read_to_end(b: &mut test::Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let mut lr = repeat(1).take(10000000);
|
|
|
|
let mut vec = Vec::with_capacity(1024);
|
2015-12-18 13:29:49 +01:00
|
|
|
super::read_to_end(&mut lr, &mut vec)
|
2015-07-10 17:34:07 +01:00
|
|
|
});
|
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|