Skip to content
Closed
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
41 changes: 41 additions & 0 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,37 @@ pub trait BufRead: Read {
#[stable(feature = "rust1", since = "1.0.0")]
fn fill_buf(&mut self) -> Result<&[u8]>;

/// Check if `Read` reached the EOF.
///
/// This function may fill the buffer to check is EOF is reached,
/// so this functions returns `Result<bool>`, not `bool`.
///
/// Default implementation calls `fill_buf` and checks that
/// returned slice is empty (which means that EOF is reached).
///
/// Examples
///
/// ```
/// #![feature(buf_read_is_eof)]
/// use std::io;
/// use std::io::prelude::*;
///
/// let stdin = io::stdin();
/// let mut stdin = stdin.lock();
///
/// while !stdin.is_eof().unwrap() {
/// let mut line = String::new();
/// stdin.read_line(&mut line).unwrap();
/// // work with line
/// println!("{:?}", line);
/// }
/// ```
#[unstable(feature = "buf_read_is_eof", reason = "recently added",
issue = "40745")]
fn is_eof(&mut self) -> Result<bool> {
self.fill_buf().map(|b| b.is_empty())
}

/// Tells this buffer that `amt` bytes have been consumed from the buffer,
/// so they should no longer be returned in calls to `read`.
///
Expand Down Expand Up @@ -1851,6 +1882,16 @@ mod tests {
assert_eq!(v, "");
}

#[test]
fn buf_read_is_eof() {
let mut buf = Cursor::new(&b"abcd"[..]);
assert!(!buf.is_eof().unwrap());
buf.read_exact(&mut [0; 2]).unwrap();
assert!(!buf.is_eof().unwrap());
buf.read_exact(&mut [0; 2]).unwrap();
assert!(buf.is_eof().unwrap());
}

#[test]
fn lines() {
let buf = Cursor::new(&b"12\r"[..]);
Expand Down