]> git.lizzy.rs Git - rust.git/commitdiff
Add capacity to BufReader with same unstable gate
authorAndreas Molzer <andreas.molzer@gmx.de>
Mon, 27 Jan 2020 18:08:53 +0000 (19:08 +0100)
committerAndreas Molzer <andreas.molzer@gmx.de>
Tue, 28 Jan 2020 13:19:44 +0000 (14:19 +0100)
src/libstd/io/buffered.rs

index 3d644b13f436147625527ca25458279f4af650bc..6739d4498a6bee522aa8bdb85646cefe88368227 100644 (file)
@@ -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<R>`, 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()
     }