[−][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.
ⓘ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 + 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]
seek_convenience
)Returns the length of this stream (in bytes). Read more
fn stream_position(&mut self) -> Result<u64, Error>
[src]
seek_convenience
)Returns the current seek position from the start of the stream. Read more
impl<W: Write, P: WriterPolicy> Write for BufWriter<W, P>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn flush(&mut self) -> Result<()>
[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 Self
1.0.0[src]
Creates a "by reference" adaptor for this instance of Write
. Read more
Auto Trait Implementations
impl<W, P> Unpin for BufWriter<W, P> where
P: Unpin,
W: Unpin,
P: Unpin,
W: Unpin,
impl<W, P> Sync for BufWriter<W, P> where
P: Sync,
W: Sync,
P: Sync,
W: Sync,
impl<W, P> Send for BufWriter<W, P> where
P: Send,
W: Send,
P: Send,
W: Send,
impl<W, P> UnwindSafe for BufWriter<W, P> where
P: UnwindSafe,
W: UnwindSafe,
P: UnwindSafe,
W: UnwindSafe,
impl<W, P> RefUnwindSafe for BufWriter<W, P> where
P: RefUnwindSafe,
W: RefUnwindSafe,
P: RefUnwindSafe,
W: RefUnwindSafe,
Blanket Implementations
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>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,