Skip to content

Commit 58557fa

Browse files
committed
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
2 parents 74be072 + df4b781 commit 58557fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/libstd/io/buffered.rs

+25
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,31 @@ impl<R: Read> BufReader<R> {
147147
#[stable(feature = "rust1", since = "1.0.0")]
148148
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
149149

150+
/// Returns `true` if there are no bytes in the internal buffer.
151+
///
152+
/// # Examples
153+
/// ```
154+
/// # #![feature(bufreader_is_empty)]
155+
/// use std::io::BufReader;
156+
/// use std::io::BufRead;
157+
/// use std::fs::File;
158+
///
159+
/// # fn foo() -> std::io::Result<()> {
160+
/// let f1 = File::open("log.txt")?;
161+
/// let mut reader = BufReader::new(f1);
162+
/// assert!(reader.is_empty());
163+
///
164+
/// if reader.fill_buf()?.len() > 0 {
165+
/// assert!(!reader.is_empty());
166+
/// }
167+
/// # Ok(())
168+
/// # }
169+
/// ```
170+
#[unstable(feature = "bufreader_is_empty", issue = "45323", reason = "recently added")]
171+
pub fn is_empty(&self) -> bool {
172+
self.pos == self.cap
173+
}
174+
150175
/// Unwraps this `BufReader`, returning the underlying reader.
151176
///
152177
/// Note that any leftover data in the internal buffer is lost.

0 commit comments

Comments
 (0)