Skip to content

Commit aebd0d7

Browse files
committed
Add capacity to BufReader with same unstable gate
1 parent 47ae565 commit aebd0d7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/libstd/io/buffered.rs

+26
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,30 @@ impl<R> BufReader<R> {
179179
&self.buf[self.pos..self.cap]
180180
}
181181

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+
182206
/// Unwraps this `BufReader<R>`, returning the underlying reader.
183207
///
184208
/// Note that any leftover data in the internal buffer is lost. Therefore,
@@ -581,6 +605,7 @@ impl<W: Write> BufWriter<W> {
581605
/// # Examples
582606
///
583607
/// ```no_run
608+
/// #![feature(buffered_io_capacity)]
584609
/// use std::io::BufWriter;
585610
/// use std::net::TcpStream;
586611
///
@@ -591,6 +616,7 @@ impl<W: Write> BufWriter<W> {
591616
/// // Calculate how many bytes can be written without flushing
592617
/// let without_flush = capacity - buf_writer.buffer().len();
593618
/// ```
619+
#[unstable(feature = "buffered_io_capacity", issue = "68558")]
594620
pub fn capacity(&self) -> usize {
595621
self.buf.capacity()
596622
}

0 commit comments

Comments
 (0)