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>

source

pub fn new(buffer: &'a mut BufWriter<W>) -> Self

source

fn inner(&self) -> &W

Get a reference to the inner writer (that is, the writer wrapped by the BufWriter).

source

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.

source

fn buffered(&self) -> &[u8]

Get the content currently buffered in self.buffer

source

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: Debug + ?Sized + Write> Debug for LineWriterShim<'a, W>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, W: ?Sized + Write> Write for LineWriterShim<'a, W>

source§

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>

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<()>

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<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector #69941)
Determines if this Writer has an efficient write_vectored implementation. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>

🔬This is a nightly-only experimental API. (write_all_vectored #70436)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<'a, W: ?Sized> RefUnwindSafe for LineWriterShim<'a, W>where W: RefUnwindSafe,

§

impl<'a, W: ?Sized> Send for LineWriterShim<'a, W>where W: Send,

§

impl<'a, W: ?Sized> Sync for LineWriterShim<'a, W>where W: Sync,

§

impl<'a, W: ?Sized> Unpin for LineWriterShim<'a, W>

§

impl<'a, W> !UnwindSafe for LineWriterShim<'a, W>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.