Struct DeferredWriter

Source
pub struct DeferredWriter<'a> { /* private fields */ }
Expand description

A buffered writer with deferred error checking.

This can be used like std::io::BufWriter, but like DeferredReader this performs deferred error checking. This means that any call to write, will always succeed. IO errors that occur during writing will be reported during the next call to flush or check_io_error. Any data written after an IO error occured, before it is eventually reported, will be discarded.

Deferring error checks like this can result in a significant speed up for some usage patterns.

Implementations§

Source§

impl<'a> DeferredWriter<'a>

Source

pub fn from_write(write: impl Write + 'a) -> Self

Creates a DeferredWriter writing data to a Write instance.

Source

pub fn from_boxed_dyn_write(write: Box<dyn Write + 'a>) -> Self

Creates a DeferredWriter writing data to a boxed Write instance.

Source

pub fn flush_defer_err(&mut self)

Flush the buffered data to the underlying Write instance, deferring IO errors.

Source

pub fn write_all_defer_err(&mut self, buf: &[u8])

Write a slice of bytes, deferring IO errors.

Both, write and write_all directly call this method. Unlike them, this does not return a #[must_use] value, making it clear that this cannot return an error.

Source

pub fn buf_write_ptr(&mut self, len: usize) -> *mut u8

Returns a pointer to the current write pointer within the internal buffer if sufficient space is available.

If only fewer than len bytes are available, this returns a null pointer.

Can be used in conjunction with advance_unchecked to construct output data directly within the output buffer, potentially avoiding a redundant copy.

Source

pub unsafe fn advance_unchecked(&mut self, len: usize)

Advances the write pointer within the internal buffer.

§Safety

This assumes that a) there is sufficient space left in the buffer and b) that the bytes the pointer is advanced over were initialized prior to calling this (via buf_write_ptr).

If either assumption does not hold calling this results in undefined behavior.

Source

pub fn check_io_error(&mut self) -> Result<()>

Returns an encountered IO errors as Err(io_err).

This resets the stored IO error and returns Ok(()) if no IO error is stored.

Trait Implementations§

Source§

impl<'a> Drop for DeferredWriter<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> Write for DeferredWriter<'a>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

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

fn write_all(&mut self, buf: &[u8]) -> Result<()>

Attempts to write an entire buffer into this writer. Read more
1.36.0 · Source§

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

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

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

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

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

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

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

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

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

Auto Trait Implementations§

§

impl<'a> Freeze for DeferredWriter<'a>

§

impl<'a> !RefUnwindSafe for DeferredWriter<'a>

§

impl<'a> !Send for DeferredWriter<'a>

§

impl<'a> !Sync for DeferredWriter<'a>

§

impl<'a> Unpin for DeferredWriter<'a>

§

impl<'a> !UnwindSafe for DeferredWriter<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.