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.
|
|
|
|
|
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;
|
deprecate Unicode functions that will be moved to crates.io
This patch
1. renames libunicode to librustc_unicode,
2. deprecates several pieces of libunicode (see below), and
3. removes references to deprecated functions from
librustc_driver and libsyntax. This may change pretty-printed
output from these modules in cases involving wide or combining
characters used in filenames, identifiers, etc.
The following functions are marked deprecated:
1. char.width() and str.width():
--> use unicode-width crate
2. str.graphemes() and str.grapheme_indices():
--> use unicode-segmentation crate
3. str.nfd_chars(), str.nfkd_chars(), str.nfc_chars(), str.nfkc_chars(),
char.compose(), char.decompose_canonical(), char.decompose_compatible(),
char.canonical_combining_class():
--> use unicode-normalization crate
2015-04-14 15:52:37 -04:00
|
|
|
use rustc_unicode::str as core_str;
|
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
|
|
|
use error as std_error;
|
2015-01-31 20:24:36 -08:00
|
|
|
use fmt;
|
2015-07-07 13:50:23 +02:00
|
|
|
use iter::{Iterator};
|
2015-01-31 20:24:36 -08:00
|
|
|
use marker::Sized;
|
|
|
|
use ops::{Drop, FnOnce};
|
|
|
|
use option::Option::{self, Some, None};
|
|
|
|
use result::Result::{Ok, Err};
|
|
|
|
use result;
|
|
|
|
use string::String;
|
2015-03-10 23:13:24 -05:00
|
|
|
use str;
|
2015-01-31 20:24:36 -08:00
|
|
|
use vec::Vec;
|
|
|
|
|
|
|
|
pub use self::buffered::{BufReader, BufWriter, BufStream, LineWriter};
|
|
|
|
pub use self::buffered::IntoInnerError;
|
|
|
|
pub use self::cursor::Cursor;
|
|
|
|
pub use self::error::{Result, Error, ErrorKind};
|
|
|
|
pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat};
|
2015-03-09 00:30:15 +02:00
|
|
|
pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr};
|
2015-02-24 23:27:20 -08:00
|
|
|
pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
|
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
|
|
|
|
|
|
|
const DEFAULT_BUF_SIZE: usize = 64 * 1024;
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
|
|
|
struct Guard<'a> { s: &'a mut Vec<u8>, len: usize }
|
2015-04-27 14:10:49 -07:00
|
|
|
impl<'a> Drop for Guard<'a> {
|
2015-01-31 20:24:36 -08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { self.s.set_len(self.len); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut g = Guard { len: buf.len(), s: buf.as_mut_vec() };
|
|
|
|
let ret = f(g.s);
|
|
|
|
if str::from_utf8(&g.s[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 {
|
|
|
|
g.len = g.s.len();
|
|
|
|
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
|
|
|
|
// time is 4,500 times (!) slower than this if the reader has a very small
|
|
|
|
// amount of data to return.
|
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> {
|
2015-03-28 18:47:29 -07:00
|
|
|
let start_len = buf.len();
|
|
|
|
let mut len = start_len;
|
2015-03-30 13:59:32 -04:00
|
|
|
let mut new_write_size = 16;
|
2015-03-28 18:47:29 -07:00
|
|
|
let ret;
|
2015-01-31 20:24:36 -08:00
|
|
|
loop {
|
2015-03-28 18:47:29 -07:00
|
|
|
if len == buf.len() {
|
2015-03-30 13:59:32 -04:00
|
|
|
if new_write_size < DEFAULT_BUF_SIZE {
|
|
|
|
new_write_size *= 2;
|
2015-03-28 18:47:29 -07:00
|
|
|
}
|
2015-07-07 13:50:23 +02:00
|
|
|
buf.resize(len + new_write_size, 0);
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|
2015-03-28 18:47:29 -07:00
|
|
|
|
|
|
|
match r.read(&mut buf[len..]) {
|
|
|
|
Ok(0) => {
|
|
|
|
ret = Ok(len - start_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(n) => 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
|
|
|
|
|
|
|
buf.truncate(len);
|
|
|
|
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
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// Implementors of the `Read` trait are sometimes called 'readers'.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// Readers are defined by one required method, `read()`. Each call to `read`
|
|
|
|
/// will attempt to pull bytes from this source into a provided buffer. A
|
|
|
|
/// number of other methods are implemented in terms of `read()`, giving
|
|
|
|
/// 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
|
|
|
|
/// throughout `std::io` take and provide types which implement the `Read`
|
|
|
|
/// trait.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::fs::File;
|
|
|
|
/// use std::io::Read;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer = Vec::new();
|
|
|
|
///
|
|
|
|
/// // read some bytes
|
|
|
|
/// f.read(&mut buffer).unwrap();
|
|
|
|
///
|
|
|
|
/// // read the whole file
|
|
|
|
/// f.read_to_end(&mut buffer).unwrap();
|
|
|
|
///
|
|
|
|
/// // 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).unwrap();
|
|
|
|
///
|
|
|
|
/// // and more! See the other methods for more details.
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-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
|
|
|
|
/// it will typically signal this via an `Err` return value.
|
|
|
|
///
|
|
|
|
/// If the return value of this method is `Ok(n)`, then it must be
|
|
|
|
/// 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
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer = [0; 10];
|
|
|
|
///
|
|
|
|
/// // read 10 bytes
|
|
|
|
/// try!(f.read(&mut buffer[..]));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 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>;
|
|
|
|
|
|
|
|
/// 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
|
2015-04-13 22:21:51 +03: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
|
|
|
|
/// `ErrorKind::Interrupted` then the error is ignored and the operation
|
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer = Vec::new();
|
|
|
|
///
|
|
|
|
/// // read the whole file
|
|
|
|
/// try!(f.read_to_end(&mut buffer));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read all bytes until EOF in this source, placing them into `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
|
|
|
/// 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.
|
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// See [`read_to_end()`][readtoend] for other error semantics.
|
|
|
|
///
|
|
|
|
/// [readtoend]: #method.read_to_end
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
///
|
|
|
|
/// try!(f.read_to_string(&mut buffer));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 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-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`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::Read;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer = Vec::new();
|
|
|
|
/// let mut other_buffer = Vec::new();
|
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let reference = f.by_ref();
|
|
|
|
///
|
|
|
|
/// // read at most 5 bytes
|
|
|
|
/// try!(reference.take(5).read_to_end(&mut buffer));
|
|
|
|
///
|
|
|
|
/// } // drop our &mut reference so we can use f again
|
|
|
|
///
|
|
|
|
/// // original file still usable, read the rest
|
|
|
|
/// try!(f.read_to_end(&mut other_buffer));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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-04-13 10:21:32 -04:00
|
|
|
/// Transforms this `Read` instance to an `Iterator` over its bytes.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// The returned type implements `Iterator` where the `Item` is `Result<u8,
|
|
|
|
/// R::Err>`. The yielded item is `Ok` if a byte was successfully read and
|
|
|
|
/// `Err` otherwise for I/O errors. EOF is mapped to returning `None` from
|
|
|
|
/// this iterator.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
///
|
|
|
|
/// for byte in f.bytes() {
|
|
|
|
/// println!("{}", byte.unwrap());
|
|
|
|
/// }
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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
|
|
|
/// Transforms this `Read` instance to an `Iterator` over `char`s.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-04-30 21:38:01 -04:00
|
|
|
/// This adaptor will attempt to interpret this reader as a UTF-8 encoded
|
2015-01-31 20:24:36 -08:00
|
|
|
/// sequence of characters. The returned iterator will return `None` once
|
|
|
|
/// EOF is reached for this reader. Otherwise each element yielded will be a
|
|
|
|
/// `Result<char, E>` where `E` may contain information about what I/O error
|
|
|
|
/// occurred or where decoding failed.
|
|
|
|
///
|
|
|
|
/// Currently this adaptor will discard intermediate data read, and should
|
|
|
|
/// be avoided if this is not desired.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(io)]
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
///
|
|
|
|
/// for c in f.chars() {
|
|
|
|
/// println!("{}", c.unwrap());
|
|
|
|
/// }
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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
|
|
|
#[unstable(feature = "io", reason = "the semantics of a partial read/write \
|
|
|
|
of where errors happen is currently \
|
|
|
|
unclear and may change")]
|
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 chars(self) -> Chars<Self> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Chars { 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`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f1 = try!(File::open("foo.txt"));
|
|
|
|
/// let mut f2 = try!(File::open("bar.txt"));
|
|
|
|
///
|
|
|
|
/// let mut handle = f1.chain(f2);
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
///
|
|
|
|
/// // read the value into a String. We could use any Read method here,
|
|
|
|
/// // this is just one example.
|
|
|
|
/// try!(handle.read_to_string(&mut buffer));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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
|
|
|
|
/// `limit` bytes, after which it will always return EOF (`Ok(0)`). Any
|
|
|
|
/// read errors will not count towards the number of bytes read and future
|
|
|
|
/// calls to `read` may succeed.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer = [0; 10];
|
|
|
|
///
|
|
|
|
/// // read at most five bytes
|
|
|
|
/// let mut handle = f.take(5);
|
|
|
|
///
|
|
|
|
/// try!(handle.read(&mut buffer));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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 }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a reader adaptor which will write all read data into the given
|
|
|
|
/// output stream.
|
|
|
|
///
|
|
|
|
/// Whenever the returned `Read` instance is read it will write the read
|
|
|
|
/// data to `out`. The current semantics of this implementation imply that
|
|
|
|
/// a `write` error will not report how much data was initially read.
|
2015-07-09 16:23:27 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// [`File`][file]s implement `Read`:
|
|
|
|
///
|
|
|
|
/// [file]: ../std/fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(io)]
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut f = try!(File::open("foo.txt"));
|
|
|
|
/// let mut buffer1 = Vec::with_capacity(10);
|
|
|
|
/// let mut buffer2 = Vec::with_capacity(10);
|
|
|
|
///
|
|
|
|
/// // write the output to buffer1 as we read
|
|
|
|
/// let mut handle = f.tee(&mut buffer1);
|
|
|
|
///
|
|
|
|
/// try!(handle.read(&mut buffer2));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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
|
|
|
#[unstable(feature = "io", reason = "the semantics of a partial read/write \
|
|
|
|
of where errors happen is currently \
|
|
|
|
unclear and may change")]
|
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 tee<W: Write>(self, out: W) -> Tee<Self, W> where Self: Sized {
|
2015-01-31 20:24:36 -08:00
|
|
|
Tee { reader: self, writer: out }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A trait for objects which are byte-oriented sinks.
|
|
|
|
///
|
|
|
|
/// The `write` method will attempt to write some data into the object,
|
|
|
|
/// returning how many bytes were successfully written.
|
|
|
|
///
|
|
|
|
/// The `flush` method is useful for adaptors and explicit buffers themselves
|
|
|
|
/// for ensuring that all buffered data has been pushed out to the "true sink".
|
|
|
|
///
|
2015-07-09 16:23:27 -04:00
|
|
|
/// * The `write()` method will attempt to write some data into the object,
|
|
|
|
/// returning how many bytes were successfully written.
|
|
|
|
///
|
|
|
|
/// * The `flush()` method is useful for adaptors and explicit buffers
|
|
|
|
/// 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
|
|
|
|
/// throughout `std::io` take and provide types which implement the `Write`
|
|
|
|
/// trait.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut buffer = try!(File::create("foo.txt"));
|
|
|
|
///
|
|
|
|
/// try!(buffer.write(b"some bytes"));
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 14:16:46 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-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
|
2015-01-31 20:24:36 -08:00
|
|
|
/// an `Err` variant.
|
|
|
|
///
|
|
|
|
/// If the return value is `Ok(n)` then it must be guaranteed that
|
|
|
|
/// `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.
|
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-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.
|
|
|
|
///
|
|
|
|
/// This method will continuously call `write` while there is more data to
|
|
|
|
/// write. This method will not return until the entire buffer has been
|
|
|
|
/// successfully written or an error occurs. The first error generated from
|
|
|
|
/// this method will be returned.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return the first error that `write` returns.
|
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.
|
|
|
|
///
|
|
|
|
/// This method is primarily used to interface with the `format_args!`
|
|
|
|
/// macro, but it is rare that this should explicitly be called. The
|
|
|
|
/// `write!` macro should be favored to invoke this method instead.
|
|
|
|
///
|
|
|
|
/// This function internally uses the `write_all` 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.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return any I/O error reported while formatting.
|
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(()),
|
|
|
|
Err(..) => output.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
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
|
|
|
|
|
|
|
/// Creates a new writer which will write all data to both this writer and
|
|
|
|
/// another writer.
|
|
|
|
///
|
|
|
|
/// All data written to the returned writer will both be written to `self`
|
|
|
|
/// as well as `other`. Note that the error semantics of the current
|
|
|
|
/// implementation do not precisely track where errors happen. For example
|
|
|
|
/// an error on the second call to `write` will not report that the first
|
|
|
|
/// call to `write` succeeded.
|
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
|
|
|
#[unstable(feature = "io", reason = "the semantics of a partial read/write \
|
|
|
|
of where errors happen is currently \
|
|
|
|
unclear and may change")]
|
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 broadcast<W: Write>(self, other: W) -> Broadcast<Self, W>
|
|
|
|
where Self: Sized
|
|
|
|
{
|
2015-01-31 20:24:36 -08:00
|
|
|
Broadcast { first: self, second: other }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An object implementing `Seek` internally has some form of cursor which can
|
|
|
|
/// be moved within a stream of bytes.
|
|
|
|
///
|
|
|
|
/// The stream typically has a fixed size, allowing seeking relative to either
|
|
|
|
/// end or the current offset.
|
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 {
|
|
|
|
/// Seek to an offset, in bytes, in a stream
|
|
|
|
///
|
|
|
|
/// A seek beyond the end of a stream is allowed, but seeking before offset
|
|
|
|
/// 0 is an error.
|
|
|
|
///
|
2015-02-16 21:03:39 -08:00
|
|
|
/// The behavior when seeking past the end of the stream is implementation
|
|
|
|
/// defined.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// This method returns the new position within the stream if the seek
|
|
|
|
/// operation completed successfully.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Seeking to a negative offset is considered an error
|
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.
|
|
|
|
#[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")]
|
2015-01-31 20:24:36 -08:00
|
|
|
Start(u64),
|
|
|
|
|
|
|
|
/// Set the offset to the size of this object plus the specified number of
|
|
|
|
/// bytes.
|
|
|
|
///
|
|
|
|
/// It is possible to seek beyond the end of an object, but is an error to
|
|
|
|
/// seek before byte 0.
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
End(i64),
|
|
|
|
|
|
|
|
/// Set the offset to the current position plus the specified number of
|
|
|
|
/// bytes.
|
|
|
|
///
|
|
|
|
/// It is possible to seek beyond the end of an object, but is an error to
|
|
|
|
/// seek before byte 0.
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-31 20:24:36 -08:00
|
|
|
Current(i64),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
match available.position_elem(&delim) {
|
|
|
|
Some(i) => {
|
|
|
|
buf.push_all(&available[..i + 1]);
|
|
|
|
(true, i + 1)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
buf.push_all(available);
|
|
|
|
(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
|
2015-07-09 14:04:30 -04:00
|
|
|
/// [`read_line()`][readline] method as well as a [`lines()`][lines] iterator.
|
|
|
|
///
|
|
|
|
/// [readline]: #method.read_line
|
|
|
|
/// [lines]: #method.lines
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::BufRead;
|
|
|
|
///
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// for line in stdin.lock().lines() {
|
|
|
|
/// println!("{}", line.unwrap());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If you have something that implements `Read`, you can use the [`BufReader`
|
|
|
|
/// type][bufreader] to turn it into a `BufRead`.
|
|
|
|
///
|
|
|
|
/// For example, [`File`][file] implements `Read`, but not `BufRead`.
|
|
|
|
/// `BufReader` to the rescue!
|
|
|
|
///
|
|
|
|
/// [bufreader]: struct.BufReader.html
|
|
|
|
/// [file]: ../fs/struct.File.html
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::fs::File;
|
|
|
|
/// use std::io::BufRead;
|
|
|
|
/// use std::io::BufReader;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let f = try!(File::open("foo.txt"));
|
|
|
|
/// let f = BufReader::new(f);
|
|
|
|
///
|
|
|
|
/// for line in f.lines() {
|
|
|
|
/// println!("{}", line.unwrap());
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
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
|
|
|
|
/// [`consume`][consume] method to function properly. When calling this
|
|
|
|
/// method, none of the contents will be "read" in the sense that later
|
|
|
|
/// calling `read` may return the same contents. As such, `consume` must be
|
|
|
|
/// called with the number of bytes that are consumed from this buffer to
|
|
|
|
/// ensure that the bytes are never returned twice.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
2015-07-09 14:04:30 -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`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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
|
|
|
|
/// [`fill_buf`][fillbuf] method to function properly. This function does
|
|
|
|
/// not perform any I/O, it simply informs this object that some amount of
|
|
|
|
/// its buffer, returned from `fill_buf`, has been consumed and should no
|
|
|
|
/// longer be returned. As such, this function may do odd things if
|
|
|
|
/// `fill_buf` isn't called before calling it.
|
|
|
|
///
|
|
|
|
/// [fillbuf]: #tymethod.fill_buff
|
|
|
|
///
|
|
|
|
/// The `amt` must be `<=` the number of bytes in the buffer returned by
|
|
|
|
/// `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
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf],
|
|
|
|
/// that method's example includes an example of `consume()`.
|
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);
|
|
|
|
|
2015-07-09 14:04:30 -04:00
|
|
|
/// Read all bytes into `buf` until the delimiter `byte` 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
|
|
|
///
|
2015-07-09 14:04:30 -04:00
|
|
|
/// If this reader is currently at EOF then this function will not modify
|
|
|
|
/// `buf` and will return `Ok(n)` where `n` is the number of bytes which
|
|
|
|
/// were read.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will ignore all instances of `ErrorKind::Interrupted` and
|
|
|
|
/// will otherwise return any errors returned by `fill_buf`.
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`. In this example, we'll
|
|
|
|
/// read from standard input until we see an `a` byte.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
|
|
|
/// fn foo() -> io::Result<()> {
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut stdin = stdin.lock();
|
|
|
|
/// let mut buffer = Vec::new();
|
|
|
|
///
|
|
|
|
/// try!(stdin.read_until(b'a', &mut buffer));
|
|
|
|
///
|
|
|
|
/// println!("{:?}", buffer);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize the `io` module
The new `std::io` module has had some time to bake now, and this commit
stabilizes its functionality. There are still portions of the module which
remain unstable, and below contains a summart of the actions taken.
This commit also deprecates the entire contents of the `old_io` module in a
blanket fashion. All APIs should now have a reasonable replacement in the
new I/O modules.
Stable APIs:
* `std::io` (the name)
* `std::io::prelude` (the name)
* `Read`
* `Read::read`
* `Read::{read_to_end, read_to_string}` after being modified to return a `usize`
for the number of bytes read.
* `Write`
* `Write::write`
* `Write::{write_all, write_fmt}`
* `BufRead`
* `BufRead::{fill_buf, consume}`
* `BufRead::{read_line, read_until}` after being modified to return a `usize`
for the number of bytes read.
* `BufReader`
* `BufReader::{new, with_capacity}`
* `BufReader::{get_ref, get_mut, into_inner}`
* `{Read,BufRead} for BufReader`
* `BufWriter`
* `BufWriter::{new, with_capacity}`
* `BufWriter::{get_ref, get_mut, into_inner}`
* `Write for BufWriter`
* `IntoInnerError`
* `IntoInnerError::{error, into_inner}`
* `{Error,Display} for IntoInnerError`
* `LineWriter`
* `LineWriter::{new, with_capacity}` - `with_capacity` was added
* `LineWriter::{get_ref, get_mut, into_inner}` - `get_mut` was added)
* `Write for LineWriter`
* `BufStream`
* `BufStream::{new, with_capacities}`
* `BufStream::{get_ref, get_mut, into_inner}`
* `{BufRead,Read,Write} for BufStream`
* `stdin`
* `Stdin`
* `Stdin::lock`
* `Stdin::read_line` - added method
* `StdinLock`
* `Read for Stdin`
* `{Read,BufRead} for StdinLock`
* `stdout`
* `Stdout`
* `Stdout::lock`
* `StdoutLock`
* `Write for Stdout`
* `Write for StdoutLock`
* `stderr`
* `Stderr`
* `Stderr::lock`
* `StderrLock`
* `Write for Stderr`
* `Write for StderrLock`
* `io::Result`
* `io::Error`
* `io::Error::last_os_error`
* `{Display, Error} for Error`
Unstable APIs:
(reasons can be found in the commit itself)
* `Write::flush`
* `Seek`
* `ErrorKind`
* `Error::new`
* `Error::from_os_error`
* `Error::kind`
Deprecated APIs
* `Error::description` - available via the `Error` trait
* `Error::detail` - available via the `Display` implementation
* `thread::Builder::{stdout, stderr}`
Changes in functionality:
* `old_io::stdio::set_stderr` is now a noop as the infrastructure for printing
backtraces has migrated to `std::io`.
* The `ReadExt`, `WriteExt`, and `BufReadExt` extension traits were all removed
by folding functionality into the corresponding trait.
[breaking-change]
2015-03-11 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
|
|
|
///
|
|
|
|
/// If this reader is currently at EOF then this function will not modify
|
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
|
|
|
/// `buf` and will return `Ok(n)` where `n` is the number of bytes which
|
|
|
|
/// were read.
|
2015-01-31 20:24:36 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function has the same error semantics as `read_until` and will 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
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`. In this example, we'll
|
|
|
|
/// read all of the lines from standard input. If we were to do this in
|
|
|
|
/// an actual project, the [`lines()`][lines] method would be easier, of
|
|
|
|
/// course.
|
|
|
|
///
|
|
|
|
/// [lines]: #method.lines
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
/// let mut stdin = stdin.lock();
|
|
|
|
/// let mut buffer = String::new();
|
|
|
|
///
|
|
|
|
/// while stdin.read_line(&mut buffer).unwrap() > 0 {
|
|
|
|
/// // work with buffer
|
|
|
|
/// println!("{:?}", buffer);
|
|
|
|
///
|
|
|
|
/// buffer.clear();
|
|
|
|
/// }
|
|
|
|
/// ```
|
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
|
|
|
|
/// `io::Result<Vec<u8>>`. Each vector returned will *not* have the
|
|
|
|
/// delimiter byte at the end.
|
|
|
|
///
|
|
|
|
/// This function will yield errors whenever `read_until` would have also
|
|
|
|
/// yielded an error.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`. In this example, we'll
|
|
|
|
/// read some input from standard input, splitting on commas.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
///
|
|
|
|
/// for content in stdin.lock().split(b',') {
|
|
|
|
/// println!("{:?}", content.unwrap());
|
|
|
|
/// }
|
|
|
|
/// ```
|
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
|
|
|
|
/// `io::Result<String>`. Each string returned will *not* have a newline
|
|
|
|
/// byte (the 0xA byte) at the end.
|
2015-07-09 14:04:30 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A locked standard input implements `BufRead`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
|
|
|
/// let stdin = io::stdin();
|
|
|
|
///
|
|
|
|
/// for line in stdin.lock().lines() {
|
|
|
|
/// println!("{}", line.unwrap());
|
|
|
|
/// }
|
|
|
|
/// ```
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A `Write` adaptor which will write data to multiple locations.
|
|
|
|
///
|
2015-03-24 19:37:52 +01:00
|
|
|
/// For more information, see `Write::broadcast`.
|
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Write::broadcast")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Broadcast<T, U> {
|
|
|
|
first: T,
|
|
|
|
second: U,
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:52 +01:00
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Write::broadcast")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<T: Write, U: Write> Write for Broadcast<T, U> {
|
|
|
|
fn write(&mut self, data: &[u8]) -> Result<usize> {
|
|
|
|
let n = try!(self.first.write(data));
|
|
|
|
// FIXME: what if the write fails? (we wrote something)
|
|
|
|
try!(self.second.write_all(&data[..n]));
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
self.first.flush().and(self.second.flush())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adaptor to chain together two instances of `Read`.
|
|
|
|
///
|
2015-03-24 19:37:52 +01:00
|
|
|
/// For more information, see `Read::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,
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
match try!(self.first.read(buf)) {
|
|
|
|
0 => { self.done_first = true; }
|
|
|
|
n => return Ok(n),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.second.read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reader adaptor which limits the bytes read from an underlying reader.
|
|
|
|
///
|
2015-03-24 19:37:52 +01:00
|
|
|
/// For more information, see `Read::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")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Take<T> {
|
|
|
|
inner: T,
|
|
|
|
limit: u64,
|
|
|
|
}
|
|
|
|
|
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> Take<T> {
|
|
|
|
/// Returns the number of bytes that can be read before this instance will
|
|
|
|
/// return EOF.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
2015-02-04 23:00:02 -05:00
|
|
|
/// This instance may reach EOF after reading fewer bytes than indicated by
|
2015-01-31 20:24:36 -08:00
|
|
|
/// this method if the underlying `Read` instance reaches EOF.
|
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 }
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
let n = try!(self.inner.read(&mut buf[..max]));
|
|
|
|
self.limit -= n as u64;
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]> {
|
|
|
|
let buf = try!(self.inner.fill_buf());
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
/// An adaptor which will emit all read data to a specified writer as well.
|
|
|
|
///
|
2015-03-24 19:37:52 +01:00
|
|
|
/// For more information see `Read::tee`
|
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::tee")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Tee<R, W> {
|
|
|
|
reader: R,
|
|
|
|
writer: W,
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:52 +01:00
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::tee")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<R: Read, W: Write> Read for Tee<R, W> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
let n = try!(self.reader.read(buf));
|
|
|
|
// FIXME: what if the write fails? (we read something)
|
|
|
|
try!(self.writer.write_all(&buf[..n]));
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A bridge from implementations of `Read` to an `Iterator` of `u8`.
|
|
|
|
///
|
2015-03-24 19:37:52 +01:00
|
|
|
/// See `Read::bytes` for more information.
|
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 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>> {
|
|
|
|
let mut buf = [0];
|
|
|
|
match self.inner.read(&mut buf) {
|
|
|
|
Ok(0) => None,
|
|
|
|
Ok(..) => Some(Ok(buf[0])),
|
|
|
|
Err(e) => Some(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A bridge from implementations of `Read` to an `Iterator` of `char`.
|
|
|
|
///
|
2015-03-24 19:37:52 +01:00
|
|
|
/// See `Read::chars` for more information.
|
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::chars")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub struct Chars<R> {
|
|
|
|
inner: R,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An enumeration of possible errors that can be generated from the `Chars`
|
|
|
|
/// adapter.
|
2015-03-31 16:01:03 -07:00
|
|
|
#[derive(Debug)]
|
2015-03-24 19:37:52 +01:00
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::chars")]
|
2015-01-31 20:24:36 -08:00
|
|
|
pub enum CharsError {
|
|
|
|
/// Variant representing that the underlying stream was read successfully
|
|
|
|
/// but it did not contain valid utf8 data.
|
|
|
|
NotUtf8,
|
|
|
|
|
|
|
|
/// Variant representing that an I/O error occurred.
|
|
|
|
Other(Error),
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:52 +01:00
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::chars")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl<R: Read> Iterator for Chars<R> {
|
|
|
|
type Item = result::Result<char, CharsError>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<result::Result<char, CharsError>> {
|
|
|
|
let mut buf = [0];
|
|
|
|
let first_byte = match self.inner.read(&mut buf) {
|
|
|
|
Ok(0) => return None,
|
|
|
|
Ok(..) => buf[0],
|
|
|
|
Err(e) => return Some(Err(CharsError::Other(e))),
|
|
|
|
};
|
|
|
|
let width = core_str::utf8_char_width(first_byte);
|
|
|
|
if width == 1 { return Some(Ok(first_byte as char)) }
|
|
|
|
if width == 0 { return Some(Err(CharsError::NotUtf8)) }
|
|
|
|
let mut buf = [first_byte, 0, 0, 0];
|
|
|
|
{
|
|
|
|
let mut start = 1;
|
|
|
|
while start < width {
|
|
|
|
match self.inner.read(&mut buf[start..width]) {
|
|
|
|
Ok(0) => return Some(Err(CharsError::NotUtf8)),
|
|
|
|
Ok(n) => start += n,
|
|
|
|
Err(e) => return Some(Err(CharsError::Other(e))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(match str::from_utf8(&buf[..width]).ok() {
|
|
|
|
Some(s) => Ok(s.char_at(0)),
|
|
|
|
None => Err(CharsError::NotUtf8),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:52 +01:00
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::chars")]
|
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
|
|
|
impl std_error::Error for CharsError {
|
2015-01-31 20:24:36 -08:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
CharsError::NotUtf8 => "invalid utf8 encoding",
|
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
|
|
|
CharsError::Other(ref e) => std_error::Error::description(e),
|
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
|
|
|
fn cause(&self) -> Option<&std_error::Error> {
|
2015-01-31 20:24:36 -08:00
|
|
|
match *self {
|
|
|
|
CharsError::NotUtf8 => None,
|
|
|
|
CharsError::Other(ref e) => e.cause(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:52 +01:00
|
|
|
#[unstable(feature = "io", reason = "awaiting stability of Read::chars")]
|
2015-01-31 20:24:36 -08:00
|
|
|
impl fmt::Display for CharsError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
CharsError::NotUtf8 => {
|
|
|
|
"byte stream did not contain valid utf8".fmt(f)
|
|
|
|
}
|
|
|
|
CharsError::Other(ref e) => e.fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over the contents of an instance of `BufRead` split on a
|
|
|
|
/// particular byte.
|
|
|
|
///
|
2015-05-10 16:32:18 -04:00
|
|
|
/// See `BufRead::split` for more information.
|
2015-03-26 16:54:15 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over the lines of an instance of `BufRead` split on a newline
|
|
|
|
/// byte.
|
|
|
|
///
|
2015-05-10 16:32:18 -04:00
|
|
|
/// See `BufRead::lines` for more information.
|
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 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();
|
|
|
|
}
|
|
|
|
Some(Ok(buf))
|
|
|
|
}
|
|
|
|
Err(e) => Some(Err(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use prelude::v1::*;
|
|
|
|
use io::prelude::*;
|
2015-02-21 11:15:58 -08:00
|
|
|
use io;
|
2015-01-31 20:24:36 -08:00
|
|
|
use super::Cursor;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
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-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.lines();
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
|
|
|
|
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"12\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
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buf = [0; 1];
|
2015-03-31 16:20:09 -07:00
|
|
|
assert_eq!(0, R.take(0).read(&mut buf).unwrap());
|
2015-02-21 11:15:58 -08:00
|
|
|
}
|
2015-01-31 20:24:36 -08:00
|
|
|
}
|