Struct std::io::buffered::linewritershim::LineWriterShim
source · pub struct LineWriterShim<'a, W: ?Sized + Write> {
buffer: &'a mut BufWriter<W>,
}
Expand description
Private helper struct for implementing the line-buffered writing logic.
This shim temporarily wraps a BufWriter, and uses its internals to
implement a line-buffered writer (specifically by using the internal
methods like write_to_buf and flush_buf). In this way, a more
efficient abstraction can be created than one that only had access to
write
and flush
, without needlessly duplicating a lot of the
implementation details of BufWriter. This also allows existing
BufWriters
to be temporarily given line-buffering logic; this is what
enables Stdout to be alternately in line-buffered or block-buffered mode.
Fields§
§buffer: &'a mut BufWriter<W>
Implementations§
source§impl<'a, W: ?Sized + Write> LineWriterShim<'a, W>
impl<'a, W: ?Sized + Write> LineWriterShim<'a, W>
pub fn new(buffer: &'a mut BufWriter<W>) -> Self
sourcefn inner(&self) -> &W
fn inner(&self) -> &W
Get a reference to the inner writer (that is, the writer wrapped by the BufWriter).
sourcefn inner_mut(&mut self) -> &mut W
fn inner_mut(&mut self) -> &mut W
Get a mutable reference to the inner writer (that is, the writer wrapped by the BufWriter). Be careful with this writer, as writes to it will bypass the buffer.
sourcefn flush_if_completed_line(&mut self) -> Result<()>
fn flush_if_completed_line(&mut self) -> Result<()>
Flush the buffer iff the last byte is a newline (indicating that an earlier write only succeeded partially, and we want to retry flushing the buffered line before continuing with a subsequent write)
Trait Implementations§
source§impl<'a, W: ?Sized + Write> Write for LineWriterShim<'a, W>
impl<'a, W: ?Sized + Write> Write for LineWriterShim<'a, W>
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write some data into this BufReader with line buffering. This means that, if any newlines are present in the data, the data up to the last newline is sent directly to the underlying writer, and data after it is buffered. Returns the number of bytes written.
This function operates on a “best effort basis”; in keeping with the
convention of Write::write
, it makes at most one attempt to write
new data to the underlying writer. If that write only reports a partial
success, the remaining data will be buffered.
Because this function attempts to send completed lines to the underlying writer, it will also flush the existing buffer if it ends with a newline, even if the incoming data does not contain any newlines.
source§fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize>
Write some vectored data into this BufReader with line buffering. This means that, if any newlines are present in the data, the data up to and including the buffer containing the last newline is sent directly to the inner writer, and the data after it is buffered. Returns the number of bytes written.
This function operates on a “best effort basis”; in keeping with the
convention of Write::write
, it makes at most one attempt to write
new data to the underlying writer.
Because this function attempts to send completed lines to the underlying writer, it will also flush the existing buffer if it contains any newlines.
Because sorting through an array of IoSlice
can be a bit convoluted,
This method differs from write in the following ways:
- It attempts to write the full content of all the buffers up to and including the one containing the last newline. This means that it may attempt to write a partial line, that buffer has data past the newline.
- If the write only reports partial success, it does not attempt to find the precise location of the written bytes and buffer the rest.
If the underlying vector doesn’t support vectored writing, we instead
simply write the first non-empty buffer with write
. This way, we
get the benefits of more granular partial-line handling without losing
anything in efficiency
source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
Write some data into this BufReader with line buffering. This means that, if any newlines are present in the data, the data up to the last newline is sent directly to the underlying writer, and data after it is buffered.
Because this function attempts to send completed lines to the underlying writer, it will also flush the existing buffer if it contains any newlines, even if the incoming data does not contain any newlines.
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
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
write_all_vectored
#70436)