@@ -179,6 +179,30 @@ impl<R> BufReader<R> {
179
179
& self . buf [ self . pos ..self . cap ]
180
180
}
181
181
182
+ /// Returns the number of bytes the internal buffer can hold at once.
183
+ ///
184
+ /// # Examples
185
+ ///
186
+ /// ```no_run
187
+ /// #![feature(buffered_io_capacity)]
188
+ /// use std::io::{BufReader, BufRead};
189
+ /// use std::fs::File;
190
+ ///
191
+ /// fn main() -> std::io::Result<()> {
192
+ /// let f = File::open("log.txt")?;
193
+ /// let mut reader = BufReader::new(f);
194
+ ///
195
+ /// let capacity = reader.capacity();
196
+ /// let buffer = reader.fill_buf()?;
197
+ /// assert!(buffer.len() <= capacity);
198
+ /// Ok(())
199
+ /// }
200
+ /// ```
201
+ #[ unstable( feature = "buffered_io_capacity" , issue = "68558" ) ]
202
+ pub fn capacity ( & self ) -> usize {
203
+ self . buf . len ( )
204
+ }
205
+
182
206
/// Unwraps this `BufReader<R>`, returning the underlying reader.
183
207
///
184
208
/// Note that any leftover data in the internal buffer is lost. Therefore,
@@ -581,6 +605,7 @@ impl<W: Write> BufWriter<W> {
581
605
/// # Examples
582
606
///
583
607
/// ```no_run
608
+ /// #![feature(buffered_io_capacity)]
584
609
/// use std::io::BufWriter;
585
610
/// use std::net::TcpStream;
586
611
///
@@ -591,6 +616,7 @@ impl<W: Write> BufWriter<W> {
591
616
/// // Calculate how many bytes can be written without flushing
592
617
/// let without_flush = capacity - buf_writer.buffer().len();
593
618
/// ```
619
+ #[ unstable( feature = "buffered_io_capacity" , issue = "68558" ) ]
594
620
pub fn capacity ( & self ) -> usize {
595
621
self . buf . capacity ( )
596
622
}
0 commit comments