diff options
| author | Andreas Molzer <andreas.molzer@gmx.de> | 2020-01-27 19:08:53 +0100 |
|---|---|---|
| committer | Andreas Molzer <andreas.molzer@gmx.de> | 2020-01-28 14:19:44 +0100 |
| commit | aebd0d733940d62566c66a923c7b9f7078209e98 (patch) | |
| tree | 2c2510386d451f53a598a303710992ba1a869996 | |
| parent | 47ae565ed4f1b2a7cc754d4cf0af520b5e6841b9 (diff) | |
| download | rust-aebd0d733940d62566c66a923c7b9f7078209e98.tar.gz rust-aebd0d733940d62566c66a923c7b9f7078209e98.zip | |
Add capacity to BufReader with same unstable gate
| -rw-r--r-- | src/libstd/io/buffered.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 3d644b13f43..6739d4498a6 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -179,6 +179,30 @@ impl<R> BufReader<R> { &self.buf[self.pos..self.cap] } + /// Returns the number of bytes the internal buffer can hold at once. + /// + /// # Examples + /// + /// ```no_run + /// #![feature(buffered_io_capacity)] + /// use std::io::{BufReader, BufRead}; + /// use std::fs::File; + /// + /// fn main() -> std::io::Result<()> { + /// let f = File::open("log.txt")?; + /// let mut reader = BufReader::new(f); + /// + /// let capacity = reader.capacity(); + /// let buffer = reader.fill_buf()?; + /// assert!(buffer.len() <= capacity); + /// Ok(()) + /// } + /// ``` + #[unstable(feature = "buffered_io_capacity", issue = "68558")] + pub fn capacity(&self) -> usize { + self.buf.len() + } + /// Unwraps this `BufReader<R>`, returning the underlying reader. /// /// Note that any leftover data in the internal buffer is lost. Therefore, @@ -581,6 +605,7 @@ impl<W: Write> BufWriter<W> { /// # Examples /// /// ```no_run + /// #![feature(buffered_io_capacity)] /// use std::io::BufWriter; /// use std::net::TcpStream; /// @@ -591,6 +616,7 @@ impl<W: Write> BufWriter<W> { /// // Calculate how many bytes can be written without flushing /// let without_flush = capacity - buf_writer.buffer().len(); /// ``` + #[unstable(feature = "buffered_io_capacity", issue = "68558")] pub fn capacity(&self) -> usize { self.buf.capacity() } |
