about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-11-06 15:19:48 +0000
committerbors <bors@rust-lang.org>2017-11-06 15:19:48 +0000
commit58557fafae060c500394d5df13cd0cf68170903e (patch)
tree6b03d624afaf8c98b85f7e24834b93c92183538f /src/libstd/io
parent74be072068737ae3ef30be66e34c1569cf652652 (diff)
parentdf4b78160cff6ba121df35b99d6c42a3dd1de62d (diff)
downloadrust-58557fafae060c500394d5df13cd0cf68170903e.tar.gz
rust-58557fafae060c500394d5df13cd0cf68170903e.zip
Auto merge of #45369 - fintelia:patch-1, r=BurntSushi
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
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/buffered.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs
index 4ebd3554fd1..6d3fbc9d268 100644
--- a/src/libstd/io/buffered.rs
+++ b/src/libstd/io/buffered.rs
@@ -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.