Skip to content

Commit

Permalink
Add a method to query the capacity of a BufWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicKatora committed Jan 26, 2020
1 parent 698fcd3 commit 47ae565
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/libstd/io/buffered.rs
Expand Up @@ -576,6 +576,25 @@ impl<W: Write> BufWriter<W> {
&self.buf
}

/// Returns the number of bytes the internal buffer can hold without flushing.
///
/// # Examples
///
/// ```no_run
/// use std::io::BufWriter;
/// use std::net::TcpStream;
///
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
///
/// // Check the capacity of the inner buffer
/// let capacity = buf_writer.capacity();
/// // Calculate how many bytes can be written without flushing
/// let without_flush = capacity - buf_writer.buffer().len();
/// ```
pub fn capacity(&self) -> usize {
self.buf.capacity()
}

/// Unwraps this `BufWriter<W>`, returning the underlying writer.
///
/// The buffer is written out before returning the writer.
Expand Down

0 comments on commit 47ae565

Please sign in to comment.