Skip to content

Commit

Permalink
Auto merge of #45369 - fintelia:patch-1, r=BurntSushi
Browse files Browse the repository at this point in the history
Implement is_empty() for BufReader

Simple implementation of `is_empty` method for BufReader so it is possible to tell whether there is any data in its buffer.

I didn't know correct stability annotation to place on the function. Presumably there is no reason to place this feature behind a feature flag, but I wasn't sure how to tag it as an unstable feature without that.

CC: #45323
  • Loading branch information
bors committed Nov 6, 2017
2 parents 74be072 + df4b781 commit 58557fa
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ impl<R: Read> BufReader<R> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }

/// Returns `true` if there are no bytes in the internal buffer.
///
/// # Examples
/// ```
/// # #![feature(bufreader_is_empty)]
/// use std::io::BufReader;
/// use std::io::BufRead;
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let f1 = File::open("log.txt")?;
/// let mut reader = BufReader::new(f1);
/// assert!(reader.is_empty());
///
/// if reader.fill_buf()?.len() > 0 {
/// assert!(!reader.is_empty());
/// }
/// # Ok(())
/// # }
/// ```
#[unstable(feature = "bufreader_is_empty", issue = "45323", reason = "recently added")]
pub fn is_empty(&self) -> bool {
self.pos == self.cap
}

/// Unwraps this `BufReader`, returning the underlying reader.
///
/// Note that any leftover data in the internal buffer is lost.
Expand Down

0 comments on commit 58557fa

Please sign in to comment.