aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/io/mod.rs
diff options
context:
space:
mode:
authorFenrirWolf <[email protected]>2018-08-19 18:01:18 -0600
committerGitHub <[email protected]>2018-08-19 18:01:18 -0600
commit15cb3c1e91842a68a8e50e1e1a42aefab13cc25e (patch)
treea514fde042ff2a504a03305bfe0894ff8cd8d47e /ctr-std/src/io/mod.rs
parentUpdate for latest nightly 2018-06-09 (#70) (diff)
parentUpdate for nightly-2018-08-18 (diff)
downloadctru-rs-15cb3c1e91842a68a8e50e1e1a42aefab13cc25e.tar.xz
ctru-rs-15cb3c1e91842a68a8e50e1e1a42aefab13cc25e.zip
Merge pull request #73 from FenrirWolf/update-2018-08-18
Update for nightly-2018-08-18
Diffstat (limited to 'ctr-std/src/io/mod.rs')
-rw-r--r--ctr-std/src/io/mod.rs171
1 files changed, 18 insertions, 153 deletions
diff --git a/ctr-std/src/io/mod.rs b/ctr-std/src/io/mod.rs
index eba4e9f..b83f3fb 100644
--- a/ctr-std/src/io/mod.rs
+++ b/ctr-std/src/io/mod.rs
@@ -147,7 +147,7 @@
//! ```
//!
//! Note that you cannot use the [`?` operator] in functions that do not return
-//! a [`Result<T, E>`][`Result`] (e.g. `main`). Instead, you can call [`.unwrap()`]
+//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
//! or `match` on the return value to catch any possible errors:
//!
//! ```no_run
@@ -270,10 +270,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use cmp;
-use core::str as core_str;
-use error as std_error;
use fmt;
-use result;
use str;
use memchr;
use ptr;
@@ -357,19 +354,26 @@ fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
// 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.
+// time is 4,500 times (!) slower than a default reservation size of 32 if the
+// reader has a very small amount of data to return.
//
// Because we're extending the buffer with uninitialized data for trusted
// readers, we need to make sure to truncate that if any of this panics.
fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
+ read_to_end_with_reservation(r, buf, 32)
+}
+
+fn read_to_end_with_reservation<R: Read + ?Sized>(r: &mut R,
+ buf: &mut Vec<u8>,
+ reservation_size: usize) -> Result<usize>
+{
let start_len = buf.len();
let mut g = Guard { len: buf.len(), buf: buf };
let ret;
loop {
if g.len == g.buf.len() {
unsafe {
- g.buf.reserve(32);
+ g.buf.reserve(reservation_size);
let capacity = g.buf.capacity();
g.buf.set_len(capacity);
r.initializer().initialize(&mut g.buf[g.len..]);
@@ -800,53 +804,6 @@ pub trait Read {
Bytes { inner: self }
}
- /// Transforms this `Read` instance to an [`Iterator`] over [`char`]s.
- ///
- /// This adaptor will attempt to interpret this reader as a UTF-8 encoded
- /// 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.
- ///
- /// # Examples
- ///
- /// [`File`]s implement `Read`:
- ///
- /// [`File`]: ../fs/struct.File.html
- /// [`Iterator`]: ../../std/iter/trait.Iterator.html
- /// [`Result`]: ../../std/result/enum.Result.html
- /// [`char`]: ../../std/primitive.char.html
- /// [`None`]: ../../std/option/enum.Option.html#variant.None
- ///
- /// ```no_run
- /// #![feature(io)]
- /// use std::io;
- /// use std::io::prelude::*;
- /// use std::fs::File;
- ///
- /// fn main() -> io::Result<()> {
- /// let mut f = File::open("foo.txt")?;
- ///
- /// for c in f.chars() {
- /// println!("{}", c.unwrap());
- /// }
- /// Ok(())
- /// }
- /// ```
- #[unstable(feature = "io", reason = "the semantics of a partial read/write \
- of where errors happen is currently \
- unclear and may change",
- issue = "27802")]
- #[rustc_deprecated(since = "1.27.0", reason = "Use str::from_utf8 instead:
- https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples")]
- #[allow(deprecated)]
- fn chars(self) -> Chars<Self> where Self: Sized {
- Chars { inner: self }
- }
-
/// Creates an adaptor which will chain this stream with another.
///
/// The returned `Read` instance will first read all bytes from this object
@@ -1949,6 +1906,12 @@ impl<T: Read> Read for Take<T> {
unsafe fn initializer(&self) -> Initializer {
self.inner.initializer()
}
+
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
+ let reservation_size = cmp::min(self.limit, 32) as usize;
+
+ read_to_end_with_reservation(self, buf, reservation_size)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1972,7 +1935,7 @@ impl<T: BufRead> BufRead for Take<T> {
}
}
-fn read_one_byte(reader: &mut Read) -> Option<Result<u8>> {
+fn read_one_byte(reader: &mut dyn Read) -> Option<Result<u8>> {
let mut buf = [0];
loop {
return match reader.read(&mut buf) {
@@ -2005,104 +1968,6 @@ impl<R: Read> Iterator for Bytes<R> {
}
}
-/// An iterator over the `char`s of a reader.
-///
-/// This struct is generally created by calling [`chars`][chars] on a reader.
-/// Please see the documentation of `chars()` for more details.
-///
-/// [chars]: trait.Read.html#method.chars
-#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
- issue = "27802")]
-#[rustc_deprecated(since = "1.27.0", reason = "Use str::from_utf8 instead:
- https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples")]
-#[derive(Debug)]
-#[allow(deprecated)]
-pub struct Chars<R> {
- inner: R,
-}
-
-/// An enumeration of possible errors that can be generated from the `Chars`
-/// adapter.
-#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
- issue = "27802")]
-#[rustc_deprecated(since = "1.27.0", reason = "Use str::from_utf8 instead:
- https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples")]
-#[derive(Debug)]
-#[allow(deprecated)]
-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),
-}
-
-#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
- issue = "27802")]
-#[allow(deprecated)]
-impl<R: Read> Iterator for Chars<R> {
- type Item = result::Result<char, CharsError>;
-
- fn next(&mut self) -> Option<result::Result<char, CharsError>> {
- let first_byte = match read_one_byte(&mut self.inner)? {
- Ok(b) => b,
- 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(ref e) if e.kind() == ErrorKind::Interrupted => continue,
- Err(e) => return Some(Err(CharsError::Other(e))),
- }
- }
- }
- Some(match str::from_utf8(&buf[..width]).ok() {
- Some(s) => Ok(s.chars().next().unwrap()),
- None => Err(CharsError::NotUtf8),
- })
- }
-}
-
-#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
- issue = "27802")]
-#[allow(deprecated)]
-impl std_error::Error for CharsError {
- fn description(&self) -> &str {
- match *self {
- CharsError::NotUtf8 => "invalid utf8 encoding",
- CharsError::Other(ref e) => std_error::Error::description(e),
- }
- }
- fn cause(&self) -> Option<&std_error::Error> {
- match *self {
- CharsError::NotUtf8 => None,
- CharsError::Other(ref e) => e.cause(),
- }
- }
-}
-
-#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
- issue = "27802")]
-#[allow(deprecated)]
-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.
///