From aebd0d733940d62566c66a923c7b9f7078209e98 Mon Sep 17 00:00:00 2001 From: Andreas Molzer Date: Mon, 27 Jan 2020 19:08:53 +0100 Subject: [PATCH] Add capacity to BufReader with same unstable gate --- src/libstd/io/buffered.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 @@ pub fn buffer(&self) -> &[u8] { &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`, returning the underlying reader. /// /// Note that any leftover data in the internal buffer is lost. Therefore, @@ -581,6 +605,7 @@ pub fn buffer(&self) -> &[u8] { /// # Examples /// /// ```no_run + /// #![feature(buffered_io_capacity)] /// use std::io::BufWriter; /// use std::net::TcpStream; /// @@ -591,6 +616,7 @@ pub fn buffer(&self) -> &[u8] { /// // 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() }