[−][src]Struct buf_redux::BufWriter
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 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][−]
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 + Debug, P: Debug> Debug for BufWriter<W, P>
[src][+]
impl<W: Write, P> Drop for BufWriter<W, P>
[src][+]
impl<W: Write + Seek, P: WriterPolicy> Seek for BufWriter<W, P>
[src][+]
impl<W: Write, P: WriterPolicy> Write for BufWriter<W, P>
[src][+]
Auto Trait Implementations
impl<W, P> RefUnwindSafe for BufWriter<W, P> where
P: RefUnwindSafe,
W: RefUnwindSafe,
P: RefUnwindSafe,
W: RefUnwindSafe,
impl<W, P> Send for BufWriter<W, P> where
P: Send,
W: Send,
P: Send,
W: Send,
impl<W, P> Sync for BufWriter<W, P> where
P: Sync,
W: Sync,
P: Sync,
W: Sync,
impl<W, P> Unpin for BufWriter<W, P> where
P: Unpin,
W: Unpin,
P: Unpin,
W: Unpin,
impl<W, P> UnwindSafe for BufWriter<W, P> where
P: UnwindSafe,
W: UnwindSafe,
P: UnwindSafe,
W: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,