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

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);

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);

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();

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();

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]);

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

Seek to the offset, in bytes, in the underlying writer.

Seeking always writes out the internal buffer before seeking.

Attempt to write bytes from buf into the object. Read more

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more

Attempt to close the object. Read more

Attempt to write bytes from bufs into the object using vectored IO operations. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Seeks to a new position in a byte stream. Read more

Writes some bytes into the byte stream. Read more

Like write(), except that it writes a slice of buffers. Read more

Writes an entire buffer into the byte stream. Read more

Flushes the stream to ensure that all buffered contents reach their destination. Read more

Closes the writer. Read more

Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.