[−][src]Struct futures_lite::io::BufWriter
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 blocking::Unblock; use futures_lite::*; use std::fs::File; let file = Unblock::new(File::create("a.txt")?); let mut writer = io::BufWriter::new(file); writer.write_all(b"hello").await?; writer.flush().await?;
Implementations
impl<W: AsyncWrite> BufWriter<W>
[src]
pub fn new(inner: W) -> BufWriter<W>
[src]
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::*; let mut bytes = Vec::new(); let writer = io::BufWriter::new(&mut bytes);
pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W>
[src]
Creates a buffered writer with the specified buffer capacity.
Examples
use futures_lite::*; let mut bytes = Vec::new(); let writer = io::BufWriter::with_capacity(100, &mut bytes);
pub fn get_ref(&self) -> &W
[src]
Gets a reference to the underlying writer.
Examples
use futures_lite::*; let mut bytes = Vec::new(); let writer = io::BufWriter::new(&mut bytes); let r = writer.get_ref();
pub fn get_mut(&mut self) -> &mut W
[src]
Gets a mutable reference to the underlying writer.
It is not advisable to directly write to the underlying writer.
Examples
use futures_lite::*; let mut bytes = Vec::new(); let mut writer = io::BufWriter::new(&mut bytes); let r = writer.get_mut();
pub fn into_inner(self) -> W
[src]
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::*; let mut bytes = vec![1, 2, 3]; let mut writer = io::BufWriter::new(&mut bytes); writer.write_all(&[4]).await?; writer.flush().await?; assert_eq!(writer.into_inner(), &[1, 2, 3, 4]);
pub fn buffer(&self) -> &[u8]
[src]
Returns a reference to the internal buffer.
Examples
use futures_lite::*; let mut bytes = Vec::new(); let writer = io::BufWriter::new(&mut bytes); // The internal buffer is empty until the first write request. assert_eq!(writer.buffer(), &[]);
Trait Implementations
impl<W: AsyncWrite + AsyncSeek> AsyncSeek for BufWriter<W>
[src]
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context,
pos: SeekFrom
) -> Poll<Result<u64>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
pos: SeekFrom
) -> Poll<Result<u64>>
Seek to the offset, in bytes, in the underlying writer.
Seeking always writes out the internal buffer before seeking.
impl<W: AsyncWrite> AsyncWrite for BufWriter<W>
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
impl<W: AsyncWrite + Debug> Debug for BufWriter<W>
[src]
impl<'__pin, W> Unpin for BufWriter<W> where
__Origin<'__pin, W>: Unpin,
[src]
__Origin<'__pin, W>: Unpin,
Auto Trait Implementations
impl<W> RefUnwindSafe for BufWriter<W> where
W: RefUnwindSafe,
W: RefUnwindSafe,
impl<W> Send for BufWriter<W> where
W: Send,
W: Send,
impl<W> Sync for BufWriter<W> where
W: Sync,
W: Sync,
impl<W> UnwindSafe for BufWriter<W> where
W: UnwindSafe,
W: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
[src]
S: AsyncSeek + ?Sized,
fn seek(&mut self, pos: SeekFrom) -> SeekFuture<Self>ⓘImportant traits for SeekFuture<'_, T>
impl<'_, T: AsyncSeek + Unpin + ?Sized> Future for SeekFuture<'_, T> type Output = Result<u64>;
where
Self: Unpin,
[src]
Important traits for SeekFuture<'_, T>
impl<'_, T: AsyncSeek + Unpin + ?Sized> Future for SeekFuture<'_, T> type Output = Result<u64>;
Self: Unpin,
impl<R> AsyncWriteExt for R where
R: AsyncWrite + ?Sized,
[src]
R: AsyncWrite + ?Sized,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>ⓘImportant traits for WriteFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for WriteFuture<'_, T> type Output = Result<usize>;
where
Self: Unpin,
[src]
Important traits for WriteFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for WriteFuture<'_, T> type Output = Result<usize>;
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘImportant traits for WriteVectoredFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for WriteVectoredFuture<'_, T> type Output = Result<usize>;
where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘ
Important traits for WriteVectoredFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for WriteVectoredFuture<'_, T> type Output = Result<usize>;
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>ⓘImportant traits for WriteAllFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for WriteAllFuture<'_, T> type Output = Result<()>;
where
Self: Unpin,
[src]
Important traits for WriteAllFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for WriteAllFuture<'_, T> type Output = Result<()>;
Self: Unpin,
fn flush(&mut self) -> FlushFuture<Self>ⓘImportant traits for FlushFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for FlushFuture<'_, T> type Output = Result<()>;
where
Self: Unpin,
[src]
Important traits for FlushFuture<'_, T>
impl<'_, T: AsyncWrite + Unpin + ?Sized> Future for FlushFuture<'_, T> type Output = Result<()>;
Self: Unpin,
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> 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>,