pub struct BufStream<S: Write> { /* private fields */ }
Expand description
Wraps a Stream and buffers input and output to and from it.
It can be excessively inefficient to work directly with a Read+Write
. For
example, every call to read
or write
on TcpStream
results in a system
call. A BufStream
keeps in memory buffers of data, making large,
infrequent calls to read
and write
on the underlying Read+Write
.
The output buffer will be written out when this stream is dropped.
Implementations§
Source§impl<S: Read + Write> BufStream<S>
impl<S: Read + Write> BufStream<S>
Sourcepub fn with_capacities(
reader_cap: usize,
writer_cap: usize,
inner: S,
) -> BufStream<S> ⓘ
pub fn with_capacities( reader_cap: usize, writer_cap: usize, inner: S, ) -> BufStream<S> ⓘ
Creates a new buffered stream with explicitly listed capacities for the reader/writer buffer.
Sourcepub fn new(inner: S) -> BufStream<S> ⓘ
pub fn new(inner: S) -> BufStream<S> ⓘ
Creates a new buffered stream with the default reader/writer buffer capacities.
Sourcepub fn get_mut(&mut self) -> &mut S
pub fn get_mut(&mut self) -> &mut S
Gets a mutable reference to the underlying stream.
§Warning
It is inadvisable to read directly from or write directly to the underlying stream.
Sourcepub fn into_inner(self) -> Result<S, IntoInnerError<BufStream<S>>>
pub fn into_inner(self) -> Result<S, IntoInnerError<BufStream<S>>>
Unwraps this BufStream
, returning the underlying stream.
The internal write buffer is written out before returning the stream. Any leftover data in the read buffer is lost.
Trait Implementations§
Source§impl<S: Read + Write> BufRead for BufStream<S>
impl<S: Read + Write> BufRead for BufStream<S>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moreSource§fn read_line(&mut self, string: &mut String) -> Result<usize>
fn read_line(&mut self, string: &mut String) -> Result<usize>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte
or EOF is reached. Read moreSource§impl<S: Read + Write> Read for BufStream<S>
impl<S: Read + Write> Read for BufStream<S>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<S: Read + Write> Write for BufStream<S>
impl<S: Read + Write> Write for BufStream<S>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)