Skip to content

Add eof to MemReader and BufReader #11437

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 1 commit into from
Jan 10, 2014
Merged
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
14 changes: 12 additions & 2 deletions src/libstd/io/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ impl MemReader {
}
}

/// Tests whether this reader has read all bytes in its buffer.
///
/// If `true`, then this will no longer return bytes from `read`.
pub fn eof(&self) -> bool { self.pos == self.buf.len() }

/// Acquires an immutable reference to the underlying buffer of this
/// `MemReader`.
///
Expand All @@ -124,7 +129,7 @@ impl MemReader {

impl Reader for MemReader {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
if self.pos == self.buf.len() { return None }
if self.eof() { return None }

let write_len = min(buf.len(), self.buf.len() - self.pos);
{
Expand Down Expand Up @@ -216,11 +221,16 @@ impl<'a> BufReader<'a> {
pos: 0
}
}

/// Tests whether this reader has read all bytes in its buffer.
///
/// If `true`, then this will no longer return bytes from `read`.
pub fn eof(&self) -> bool { self.pos == self.buf.len() }
}

impl<'a> Reader for BufReader<'a> {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
if self.pos == self.buf.len() { return None }
if self.eof() { return None }

let write_len = min(buf.len(), self.buf.len() - self.pos);
{
Expand Down