Skip to content

Commit 47ae565

Browse files
committed
Add a method to query the capacity of a BufWriter
1 parent 698fcd3 commit 47ae565

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libstd/io/buffered.rs

+19
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,25 @@ impl<W: Write> BufWriter<W> {
576576
&self.buf
577577
}
578578

579+
/// Returns the number of bytes the internal buffer can hold without flushing.
580+
///
581+
/// # Examples
582+
///
583+
/// ```no_run
584+
/// use std::io::BufWriter;
585+
/// use std::net::TcpStream;
586+
///
587+
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
588+
///
589+
/// // Check the capacity of the inner buffer
590+
/// let capacity = buf_writer.capacity();
591+
/// // Calculate how many bytes can be written without flushing
592+
/// let without_flush = capacity - buf_writer.buffer().len();
593+
/// ```
594+
pub fn capacity(&self) -> usize {
595+
self.buf.capacity()
596+
}
597+
579598
/// Unwraps this `BufWriter<W>`, returning the underlying writer.
580599
///
581600
/// The buffer is written out before returning the writer.

0 commit comments

Comments
 (0)