Skip to content

Add a Reader::fill() method #13049

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

Merged
merged 2 commits into from
Mar 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 2 additions & 4 deletions src/librand/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ impl<R: Reader> Rng for ReaderRng<R> {
}
fn fill_bytes(&mut self, v: &mut [u8]) {
if v.len() == 0 { return }
match self.reader.read(v) {
Ok(n) if n == v.len() => return,
Ok(n) => fail!("ReaderRng.fill_bytes could not fill buffer: \
read {} out of {} bytes.", n, v.len()),
match self.reader.fill(v) {
Ok(()) => {}
Err(e) => fail!("ReaderRng.fill_bytes error: {}", e)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<R: Reader> BufferedReader<R> {
}

impl<R: Reader> Buffer for BufferedReader<R> {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
if self.pos == self.cap {
self.cap = try!(self.inner.read(self.buf));
self.pos = 0;
Expand All @@ -104,7 +104,7 @@ impl<R: Reader> Buffer for BufferedReader<R> {
impl<R: Reader> Reader for BufferedReader<R> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
let nread = {
let available = try!(self.fill());
let available = try!(self.fill_buf());
let nread = cmp::min(available.len(), buf.len());
slice::bytes::copy_memory(buf, available.slice_to(nread));
nread
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<S: Stream> BufferedStream<S> {
}

impl<S: Stream> Buffer for BufferedStream<S> {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> { self.inner.fill() }
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> { self.inner.fill_buf() }
fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
}

Expand Down
18 changes: 16 additions & 2 deletions src/libstd/io/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Seek for MemReader {
}

impl Buffer for MemReader {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
if self.pos < self.buf.len() {
Ok(self.buf.slice_from(self.pos))
} else {
Expand Down Expand Up @@ -322,7 +322,7 @@ impl<'a> Seek for BufReader<'a> {
}

impl<'a> Buffer for BufReader<'a> {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
if self.pos < self.buf.len() {
Ok(self.buf.slice_from(self.pos))
} else {
Expand Down Expand Up @@ -555,4 +555,18 @@ mod test {
let mut r = BufWriter::new(buf);
assert!(r.seek(-1, SeekSet).is_err());
}

#[test]
fn io_fill() {
let mut r = MemReader::new(~[1, 2, 3, 4, 5, 6, 7, 8]);
let mut buf = [0, ..3];
assert_eq!(r.fill(buf), Ok(()));
assert_eq!(buf.as_slice(), &[1, 2, 3]);
assert_eq!(r.fill(buf.mut_slice_to(0)), Ok(()));
assert_eq!(buf.as_slice(), &[1, 2, 3]);
assert_eq!(r.fill(buf), Ok(()));
assert_eq!(buf.as_slice(), &[4, 5, 6]);
assert!(r.fill(buf).is_err());
assert_eq!(buf.as_slice(), &[7, 8, 6]);
}
}
21 changes: 19 additions & 2 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,23 @@ pub trait Reader {
}
}

/// Fills the provided slice with bytes from this reader
///
/// This will continue to call `read` until the slice has been completely
/// filled with bytes.
///
/// # Error
///
/// If an error occurs at any point, that error is returned, and no further
/// bytes are read.
fn fill(&mut self, buf: &mut [u8]) -> IoResult<()> {
let mut read = 0;
while read < buf.len() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be an infinite loop if .read reads zero bytes repeatedly; is this a concern?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On unices, a 0-length read indicates EOF, and the I/O implementation layer is responsible for translating that to an error, and I imagine that windows is fairly similar.

I'm not 100% certain that a 0-length read returning infinitely is impossible, but I'm fairly certain it won't happen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are readers that exist that don't end up calling POSIX read() (or the Windows equivalent).

read += try!(self.read(buf.mut_slice_from(read)));
}
Ok(())
}

/// Reads exactly `len` bytes and appends them to a vector.
///
/// May push fewer than the requested number of bytes on error
Expand Down Expand Up @@ -1045,7 +1062,7 @@ pub trait Buffer: Reader {
/// This function will return an I/O error if the underlying reader was
/// read, but returned an error. Note that it is not an error to return a
/// 0-length buffer.
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]>;
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]>;

/// Tells this buffer that `amt` bytes have been consumed from the buffer,
/// so they should no longer be returned in calls to `fill` or `read`.
Expand Down Expand Up @@ -1116,7 +1133,7 @@ pub trait Buffer: Reader {
let mut used;
loop {
{
let available = match self.fill() {
let available = match self.fill_buf() {
Ok(n) => n,
Err(ref e) if res.len() > 0 && e.kind == EndOfFile => {
used = 0;
Expand Down