Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Read::size_hint and pre-allocate in read_to_end #45928

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change Read::size_hint return io::Result
  • Loading branch information
SimonSapin committed Nov 30, 2017
commit 1599ffc6a3e463080a3efba9c16036b92b95adfc
9 changes: 3 additions & 6 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
@@ -449,11 +449,8 @@ impl Read for File {
self.inner.read(buf)
}

fn size_hint(&self) -> usize {
match self.metadata() {
Ok(meta) => meta.len() as usize,
Err(_) => 0,
}
fn size_hint(&self) -> io::Result<usize> {
Ok(self.metadata()?.len() as usize)
}

#[inline]
@@ -480,7 +477,7 @@ impl<'a> Read for &'a File {
self.inner.read(buf)
}

fn size_hint(&self) -> usize {
fn size_hint(&self) -> io::Result<usize> {
(**self).size_hint()
}

4 changes: 2 additions & 2 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
@@ -212,9 +212,9 @@ impl<R: Read> Read for BufReader<R> {
}

#[inline]
fn size_hint(&self) -> usize {
fn size_hint(&self) -> io::Result<usize> {
let buffered_len = self.cap - self.pos;
buffered_len.saturating_add(self.inner.size_hint())
Ok(buffered_len.saturating_add(self.inner.size_hint()?))
}

// we can't skip unconditionally because of the large buffer case in read.
4 changes: 2 additions & 2 deletions src/libstd/io/cursor.rs
Original file line number Diff line number Diff line change
@@ -230,8 +230,8 @@ impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
Ok(n)
}

fn size_hint(&self) -> usize {
(self.inner.as_ref().len() as u64).saturating_sub(self.pos) as usize
fn size_hint(&self) -> io::Result<usize> {
Ok((self.inner.as_ref().len() as u64).saturating_sub(self.pos) as usize)
}

#[inline]
8 changes: 4 additions & 4 deletions src/libstd/io/impls.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R {
}

#[inline]
fn size_hint(&self) -> usize {
fn size_hint(&self) -> io::Result<usize> {
(**self).size_hint()
}

@@ -98,7 +98,7 @@ impl<R: Read + ?Sized> Read for Box<R> {
}

#[inline]
fn size_hint(&self) -> usize {
fn size_hint(&self) -> io::Result<usize> {
(**self).size_hint()
}

@@ -192,8 +192,8 @@ impl<'a> Read for &'a [u8] {
}

#[inline]
fn size_hint(&self) -> usize {
self.len()
fn size_hint(&self) -> io::Result<usize> {
Ok(self.len())
}

#[inline]
16 changes: 8 additions & 8 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
@@ -367,7 +367,7 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
let start_len = buf.len();
let mut g = Guard { len: buf.len(), buf: buf };

let size_hint = r.size_hint();
let size_hint = r.size_hint()?;
if size_hint > 0 {
unsafe {
g.buf.reserve(size_hint.saturating_add(1));
@@ -564,8 +564,8 @@ pub trait Read {
/// [`read_to_string`]: #method.read_to_string
#[unstable(feature = "read_size_hint", issue = /* FIXME */ "0")]
#[inline]
fn size_hint(&self) -> usize {
0
fn size_hint(&self) -> Result<usize> {
Ok(0)
}

/// Read all bytes until EOF in this source, placing them into `buf`.
@@ -1744,11 +1744,11 @@ impl<T: Read, U: Read> Read for Chain<T, U> {
self.second.read(buf)
}

fn size_hint(&self) -> usize {
fn size_hint(&self) -> Result<usize> {
if self.done_first {
self.second.size_hint()
} else {
self.first.size_hint().saturating_add(self.second.size_hint())
Ok(self.first.size_hint()?.saturating_add(self.second.size_hint()?))
}
}

@@ -1950,11 +1950,11 @@ impl<T: Read> Read for Take<T> {
Ok(n)
}

fn size_hint(&self) -> usize {
fn size_hint(&self) -> Result<usize> {
if self.limit == 0 {
0
Ok(0)
} else {
cmp::min(self.limit, self.inner.size_hint() as u64) as usize
Ok(cmp::min(self.limit, self.inner.size_hint()? as u64) as usize)
}
}