pub struct BufWriter<W> { /* private fields */ }
Expand description
Adds buffering to a writer.
It can be excessively inefficient to work directly with something that implements
AsyncWrite
. For example, every call to write()
on a TCP
stream results in a system call. A BufWriter
keeps an in-memory buffer of data and
writes it to the underlying writer in large, infrequent batches.
BufWriter
can improve the speed of programs that make small and repeated writes to
the same file or networking socket. It does not help when writing very large amounts at
once, or writing just once or a few times. It also provides no advantage when writing to a
destination that is in memory, like a Vec<u8>
.
Unlike std::io::BufWriter
, this type does not write out the contents of its buffer when
it is dropped. Therefore, it is important that users explicitly flush the buffer before
dropping the BufWriter
.
Examples
use futures_lite::io::{AsyncWriteExt, BufWriter};
let mut output = Vec::new();
let mut writer = BufWriter::new(&mut output);
writer.write_all(b"hello").await?;
writer.flush().await?;
Implementations
sourceimpl<W> BufWriter<W> where
W: AsyncWrite,
impl<W> BufWriter<W> where
W: AsyncWrite,
sourcepub fn new(inner: W) -> BufWriter<W>
pub fn new(inner: W) -> BufWriter<W>
Creates a buffered writer with the default buffer capacity.
The default capacity is currently 8 KB, but that may change in the future.
Examples
use futures_lite::io::BufWriter;
let mut output = Vec::new();
let writer = BufWriter::new(&mut output);
sourcepub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W>
pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W>
Creates a buffered writer with the specified buffer capacity.
Examples
use futures_lite::io::BufWriter;
let mut output = Vec::new();
let writer = BufWriter::with_capacity(100, &mut output);
sourcepub fn get_ref(&self) -> &W
pub fn get_ref(&self) -> &W
Gets a reference to the underlying writer.
Examples
use futures_lite::io::BufWriter;
let mut output = Vec::new();
let writer = BufWriter::new(&mut output);
let r = writer.get_ref();
sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&mut self) -> &mut W
Gets a mutable reference to the underlying writer.
It is not advisable to directly write to the underlying writer.
Examples
use futures_lite::io::BufWriter;
let mut output = Vec::new();
let mut writer = BufWriter::new(&mut output);
let r = writer.get_mut();
sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Unwraps the buffered writer, returning the underlying writer.
Note that any leftover data in the internal buffer will be lost. If you don’t want to lose that data, flush the buffered writer before unwrapping it.
Examples
use futures_lite::io::{AsyncWriteExt, BufWriter};
let mut output = vec![1, 2, 3];
let mut writer = BufWriter::new(&mut output);
writer.write_all(&[4]).await?;
writer.flush().await?;
assert_eq!(writer.into_inner(), &[1, 2, 3, 4]);
sourcepub fn buffer(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn buffer(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Returns a reference to the internal buffer.
Examples
use futures_lite::io::BufWriter;
let mut output = Vec::new();
let writer = BufWriter::new(&mut output);
// The internal buffer is empty until the first write request.
assert_eq!(writer.buffer(), &[]);
Trait Implementations
sourceimpl<W> AsyncSeek for BufWriter<W> where
W: AsyncWrite + AsyncSeek,
impl<W> AsyncSeek for BufWriter<W> where
W: AsyncWrite + AsyncSeek,
sourceimpl<W> AsyncWrite for BufWriter<W> where
W: AsyncWrite,
impl<W> AsyncWrite for BufWriter<W> where
W: AsyncWrite,
sourcefn poll_write(
self: Pin<&mut BufWriter<W>>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_write(
self: Pin<&mut BufWriter<W>>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
Attempt to write bytes from buf
into the object. Read more
sourcefn poll_flush(
self: Pin<&mut BufWriter<W>>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
fn poll_flush(
self: Pin<&mut BufWriter<W>>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
sourceimpl<W> Debug for BufWriter<W> where
W: AsyncWrite + Debug,
impl<W> Debug for BufWriter<W> where
W: AsyncWrite + Debug,
impl<'__pin, W> Unpin for BufWriter<W> where
__Origin<'__pin, W>: Unpin,
Auto Trait Implementations
impl<W> RefUnwindSafe for BufWriter<W> where
W: RefUnwindSafe,
impl<W> Send for BufWriter<W> where
W: Send,
impl<W> Sync for BufWriter<W> where
W: Sync,
impl<W> UnwindSafe for BufWriter<W> where
W: UnwindSafe,
Blanket Implementations
sourceimpl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
impl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
sourceimpl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
sourcefn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>ⓘNotable traits for WriteFuture<'_, W>impl<'_, W> Future for WriteFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>ⓘNotable traits for WriteFuture<'_, W>impl<'_, W> Future for WriteFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
Writes some bytes into the byte stream. Read more
sourcefn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘNotable traits for WriteVectoredFuture<'_, W>impl<'_, W> Future for WriteVectoredFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘNotable traits for WriteVectoredFuture<'_, W>impl<'_, W> Future for WriteVectoredFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
sourcefn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>ⓘNotable traits for WriteAllFuture<'_, W>impl<'_, W> Future for WriteAllFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>ⓘNotable traits for WriteAllFuture<'_, W>impl<'_, W> Future for WriteAllFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
Writes an entire buffer into the byte stream. Read more
sourcefn flush(&mut self) -> FlushFuture<'_, Self>ⓘNotable traits for FlushFuture<'_, W>impl<'_, W> Future for FlushFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>ⓘNotable traits for FlushFuture<'_, W>impl<'_, W> Future for FlushFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
Flushes the stream to ensure that all buffered contents reach their destination. Read more
sourcefn close(&mut self) -> CloseFuture<'_, Self>ⓘNotable traits for CloseFuture<'_, W>impl<'_, W> Future for CloseFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn close(&mut self) -> CloseFuture<'_, Self>ⓘNotable traits for CloseFuture<'_, W>impl<'_, W> Future for CloseFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
Closes the writer. Read more
sourcefn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a
. Read more
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more