[][src]Struct buf_redux::BufWriter

pub struct BufWriter<W: Write, P = StdPolicy> { /* fields omitted */ }

A drop-in replacement for std::io::BufWriter with more functionality.

Original method names/signatures and implemented traits are left untouched, making replacement as simple as swapping the import of the type.

By default this type implements the behavior of its std counterpart: it only flushes the buffer if an incoming write is larger than the remaining space.

To change this type's behavior, change the policy with .set_policy() using a type from the policy module or your own implentation of WriterPolicy.

Policies that perform alternating writes and flushes without completely emptying the buffer may benefit from using a ringbuffer via the new_ringbuf() and with_capacity_ringbuf() constructors. Ringbuffers are only available on supported platforms with the slice-deque feature and have some caveats; see the docs at the crate root for more details.

Methods

impl<W: Write> BufWriter<W>[src]

pub fn new(inner: W) -> Self[src]

Create a new BufWriter wrapping inner with the default buffer capacity and WriterPolicy.

pub fn with_capacity(cap: usize, inner: W) -> Self[src]

Create a new BufWriter wrapping inner, utilizing a buffer with a capacity of at least cap bytes and the default WriterPolicy.

The actual capacity of the buffer may vary based on implementation details of the global allocator.

pub fn new_ringbuf(inner: W) -> Self[src]

Create a new BufWriter wrapping inner, utilizing a ringbuffer with the default capacity and WriterPolicy.

A ringbuffer never has to move data to make room; consuming bytes from the head simultaneously makes room at the tail. This is useful in conjunction with a policy like FlushExact to ensure there is always room to write more data if necessary, without expensive copying operations.

Only available on platforms with virtual memory support and with the slice-deque feature enabled. The default capacity will differ between Windows and Unix-derivative targets. See Buffer::new_ringbuf() or the crate root docs for more info.

pub fn with_capacity_ringbuf(cap: usize, inner: W) -> Self[src]

Create a new BufWriter wrapping inner, utilizing a ringbuffer with at least cap capacity and the default WriterPolicy.

A ringbuffer never has to move data to make room; consuming bytes from the head simultaneously makes room at the tail. This is useful in conjunction with a policy like FlushExact to ensure there is always room to write more data if necessary, without expensive copying operations.

Only available on platforms with virtual memory support and with the slice-deque feature enabled. The capacity will be rounded up to the minimum size for the target platform. See Buffer::with_capacity_ringbuf() or the crate root docs for more info.

Important traits for BufWriter<W, P>
pub fn with_buffer(buf: Buffer, inner: W) -> BufWriter<W>[src]

Create a new BufWriter wrapping inner, utilizing the existing Buffer instance and the default WriterPolicy.

Note

Does not clear the buffer first! If there is data already in the buffer it will be written out on the next flush!

impl<W: Write, P> BufWriter<W, P>[src]

Important traits for BufWriter<W, P>
pub fn set_policy<P_: WriterPolicy>(self, policy: P_) -> BufWriter<W, P_>[src]

Set a new WriterPolicy, returning the transformed type.

pub fn policy_mut(&mut self) -> &mut P[src]

Mutate the current WriterPolicy.

pub fn policy(&self) -> &P[src]

Inspect the current WriterPolicy.

pub fn get_ref(&self) -> &W[src]

Get a reference to the inner writer.

pub fn get_mut(&mut self) -> &mut W[src]

Get a mutable reference to the inner writer.

Note

If the buffer has not been flushed, writing directly to the inner type will cause data inconsistency.

pub fn capacity(&self) -> usize[src]

Get the capacty of the inner buffer.

pub fn buf_len(&self) -> usize[src]

Get the number of bytes currently in the buffer.

pub fn reserve(&mut self, additional: usize)[src]

Reserve space in the buffer for at least additional bytes. May not be quite exact due to implementation details of the buffer's allocator.

pub fn make_room(&mut self)[src]

Move data to the start of the buffer, making room at the end for more writing.

This is a no-op with the *_ringbuf() constructors (requires slice-deque feature).

pub fn into_inner_with_buffer(self) -> (W, Buffer)[src]

Consume self and return both the underlying writer and the buffer

impl<W: Write, P: WriterPolicy> BufWriter<W, P>[src]

pub fn into_inner(self) -> Result<W, IntoInnerError<Self>>[src]

Flush the buffer and unwrap, returning the inner writer on success, or a type wrapping self plus the error otherwise.

pub fn into_inner_with_err(self) -> (W, Option<Error>)[src]

Flush the buffer and unwrap, returning the inner writer and any error encountered during flushing.

Trait Implementations

impl<W: Write, P> Drop for BufWriter<W, P>[src]

Attempt to flush the buffer to the underlying writer.

If an error occurs, the thread-local handler is invoked, if one was previously set by set_drop_err_handler for this thread.

impl<W: Write + Debug, P: Debug> Debug for BufWriter<W, P>[src]

impl<W: Write, P: WriterPolicy> Write for BufWriter<W, P>[src]

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

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

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

Attempts to write an entire buffer into this writer. Read more

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>1.0.0[src]

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self1.0.0[src]

Creates a "by reference" adaptor for this instance of Write. Read more

impl<W: Write + Seek, P: WriterPolicy> Seek for BufWriter<W, P>[src]

fn seek(&mut self, pos: SeekFrom) -> Result<u64>[src]

Seek to the ofPet, in bytes, in the underlying writer.

Seeking always writes out the internal buffer before seeking.

fn stream_len(&mut self) -> Result<u64, Error>[src]

🔬 This is a nightly-only experimental API. (seek_convenience)

Returns the length of this stream (in bytes). Read more

fn stream_position(&mut self) -> Result<u64, Error>[src]

🔬 This is a nightly-only experimental API. (seek_convenience)

Returns the current seek position from the start of the stream. Read more

Auto Trait Implementations

impl<W, P> Sync for BufWriter<W, P> where
    P: Sync,
    W: Sync

impl<W, P> Send for BufWriter<W, P> where
    P: Send,
    W: Send

impl<W, P> Unpin for BufWriter<W, P> where
    P: Unpin,
    W: Unpin

impl<W, P> RefUnwindSafe for BufWriter<W, P> where
    P: RefUnwindSafe,
    W: RefUnwindSafe

impl<W, P> UnwindSafe for BufWriter<W, P> where
    P: UnwindSafe,
    W: UnwindSafe

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]