Rollup merge of #49139 - sfackler:bufreader-buffer, r=SimonSapin

Add BufReader::buffer

This subsumes the need for an explicit is_empty function, and provides
access to the buffered data itself which has been requested from time to
time.

We could call this `buf` to match `fill_buf`, but I think I'd prefer `fill_buffer` anyways in hindsight.
This commit is contained in:
kennytm 2018-03-20 07:15:25 +08:00 committed by GitHub
commit 1f5d31f8db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -168,8 +168,36 @@ impl<R: Read> BufReader<R> {
/// # }
/// ```
#[unstable(feature = "bufreader_is_empty", issue = "45323", reason = "recently added")]
#[rustc_deprecated(since = "1.26.0", reason = "use .buffer().is_empty() instead")]
pub fn is_empty(&self) -> bool {
self.pos == self.cap
self.buffer().is_empty()
}
/// Returns a reference to the internally buffered data.
///
/// Unlike `fill_buf`, this will not attempt to fill the buffer if it is empty.
///
/// # Examples
///
/// ```
/// # #![feature(bufreader_buffer)]
/// use std::io::{BufReader, BufRead};
/// use std::fs::File;
///
/// # fn foo() -> std::io::Result<()> {
/// let f = File::open("log.txt")?;
/// let mut reader = BufReader::new(f);
/// assert!(reader.buffer().is_empty());
///
/// if reader.fill_buf()?.len() > 0 {
/// assert!(!reader.buffer().is_empty());
/// }
/// # Ok(())
/// # }
/// ```
#[unstable(feature = "bufreader_buffer", issue = "45323")]
pub fn buffer(&self) -> &[u8] {
&self.buf[self.pos..self.cap]
}
/// Unwraps this `BufReader`, returning the underlying reader.