pub struct BufWriter<W: Write, P = StdPolicy> { /* private fields */ }Expand description
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.
Implementations§
Source§impl<W: Write> BufWriter<W>
impl<W: Write> BufWriter<W>
Sourcepub fn new(inner: W) -> Self
pub fn new(inner: W) -> Self
Create a new BufWriter wrapping inner with the default buffer capacity and
WriterPolicy.
Sourcepub fn with_capacity(cap: usize, inner: W) -> Self
pub fn with_capacity(cap: usize, inner: W) -> Self
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.
Sourcepub fn with_buffer(buf: Buffer, inner: W) -> BufWriter<W> ⓘ
pub fn with_buffer(buf: Buffer, inner: W) -> BufWriter<W> ⓘ
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!
Source§impl<W: Write, P> BufWriter<W, P>
impl<W: Write, P> BufWriter<W, P>
Sourcepub fn set_policy<P_: WriterPolicy>(self, policy: P_) -> BufWriter<W, P_> ⓘ
pub fn set_policy<P_: WriterPolicy>(self, policy: P_) -> BufWriter<W, P_> ⓘ
Set a new WriterPolicy, returning the transformed type.
Sourcepub fn policy_mut(&mut self) -> &mut P
pub fn policy_mut(&mut self) -> &mut P
Mutate the current WriterPolicy.
Sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&mut self) -> &mut W
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.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserve space in the buffer for at least additional bytes. May not be
quite exact due to implementation details of the buffer’s allocator.
Sourcepub fn make_room(&mut self)
pub fn make_room(&mut self)
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).
Sourcepub fn into_inner_with_buffer(self) -> (W, Buffer)
pub fn into_inner_with_buffer(self) -> (W, Buffer)
Consume self and return both the underlying writer and the buffer
Source§impl<W: Write, P: WriterPolicy> BufWriter<W, P>
impl<W: Write, P: WriterPolicy> BufWriter<W, P>
Sourcepub fn into_inner(self) -> Result<W, IntoInnerError<Self>>
pub fn into_inner(self) -> Result<W, IntoInnerError<Self>>
Flush the buffer and unwrap, returning the inner writer on success,
or a type wrapping self plus the error otherwise.
Sourcepub fn into_inner_with_err(self) -> (W, Option<Error>)
pub fn into_inner_with_err(self) -> (W, Option<Error>)
Flush the buffer and unwrap, returning the inner writer and any error encountered during flushing.
Trait Implementations§
Source§impl<W: Write, P> Drop for BufWriter<W, P>
Attempt to flush the buffer to the underlying writer.
impl<W: Write, P> Drop for BufWriter<W, P>
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.
Source§impl<W: Write + Seek, P: WriterPolicy> Seek for BufWriter<W, P>
impl<W: Write + Seek, P: WriterPolicy> Seek for BufWriter<W, P>
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to the ofPet, in bytes, in the underlying writer.
Seeking always writes out the internal buffer before seeking.
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)Source§impl<W: Write, P: WriterPolicy> Write for BufWriter<W, P>
impl<W: Write, P: WriterPolicy> Write for BufWriter<W, P>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)