Struct BufReader

struct BufReader<R> { ... }

The BufReader struct adds buffering to any reader.

It can be excessively inefficient to work directly with a AsyncRead instance. A BufReader performs large, infrequent reads on the underlying AsyncRead and maintains an in-memory buffer of the results.

BufReader can improve the speed of programs that make small and repeated read calls to the same file or network socket. It does not help when reading very large amounts at once, or reading just one or a few times. It also provides no advantage when reading from a source that is already in memory, like a Vec<u8>.

When the BufReader is dropped, the contents of its buffer will be discarded. Creating multiple instances of a BufReader on the same stream can cause data loss.

Implementations

impl<R> BufReader<R>

fn get_ref(self: &Self) -> &R

Acquires a reference to the underlying sink or stream that this combinator is pulling from.

fn get_mut(self: &mut Self) -> &mut R

Acquires a mutable reference to the underlying sink or stream that this combinator is pulling from.

Note that care must be taken to avoid tampering with the state of the sink or stream which may otherwise confuse this combinator.

fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R>

Acquires a pinned mutable reference to the underlying sink or stream that this combinator is pulling from.

Note that care must be taken to avoid tampering with the state of the sink or stream which may otherwise confuse this combinator.

fn into_inner(self: Self) -> R

Consumes this combinator, returning the underlying sink or stream.

Note that this may discard intermediate state of this combinator, so care should be taken to avoid losing resources when this is called.

fn buffer(self: &Self) -> &[u8]

Returns a reference to the internally buffered data.

Unlike fill_buf, this will not attempt to fill the buffer if it is empty.

impl<R: AsyncRead + AsyncSeek> BufReader<R>

fn seek_relative(self: Pin<&mut Self>, offset: i64) -> SeeKRelative<'_, R>

Seeks relative to the current position. If the new position lies within the buffer, the buffer will not be flushed, allowing for more efficient seeks. This method does not return the location of the underlying reader, so the caller must track this information themselves if it is required.

fn poll_seek_relative(self: Pin<&mut Self>, cx: &mut Context<'_>, offset: i64) -> Poll<Result<()>>

Attempts to seek relative to the current position. If the new position lies within the buffer, the buffer will not be flushed, allowing for more efficient seeks. This method does not return the location of the underlying reader, so the caller must track this information themselves if it is required.

impl<R: AsyncRead> BufReader<R>

fn new(inner: R) -> Self

Creates a new BufReader with a default buffer capacity. The default is currently 8 KB, but may change in the future.

fn with_capacity(capacity: usize, inner: R) -> Self

Creates a new BufReader with the specified buffer capacity.

impl<'__pin, R> Unpin for BufReader<R>

impl<R> AsyncBufReadExt for BufReader<R>

impl<R> AsyncReadExt for BufReader<R>

impl<R> Freeze for BufReader<R>

impl<R> RefUnwindSafe for BufReader<R>

impl<R> Send for BufReader<R>

impl<R> Sync for BufReader<R>

impl<R> UnsafeUnpin for BufReader<R>

impl<R> UnwindSafe for BufReader<R>

impl<R: AsyncRead + AsyncSeek> AsyncSeek for BufReader<R>

fn poll_seek(self: Pin<&mut Self>, cx: &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>>

Seek to an offset, in bytes, in the underlying reader.

The position used for seeking with SeekFrom::Current(_) is the position the underlying reader would be at if the BufReader had no internal buffer.

Seeking always discards the internal buffer, even if the seek position would otherwise fall within it. This guarantees that calling .into_inner() immediately after a seek yields the underlying reader at the same position.

To seek without discarding the internal buffer, use BufReader::seek_relative or BufReader::poll_seek_relative.

See AsyncSeek for more details.

Note: In the edge case where you're seeking with SeekFrom::Current(n) where n minus the internal buffer length overflows an i64, two seeks will be performed instead of one. If the second seek returns Err, the underlying reader will be left at the same position it would have if you called seek with SeekFrom::Current(0).

impl<R: AsyncRead> AsyncBufRead for BufReader<R>

fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>>
fn consume(self: Pin<&mut Self>, amt: usize)

impl<R: AsyncRead> AsyncRead for BufReader<R>

fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>>
fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>]) -> Poll<Result<usize>>

impl<R: AsyncWrite> AsyncWrite for BufReader<R>

fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

impl<R: fmt::Debug> Debug for BufReader<R>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<S> AsyncSeekExt for BufReader<R>

impl<T> Any for BufReader<R>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for BufReader<R>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for BufReader<R>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for BufReader<R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for BufReader<R>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for BufReader<R>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for BufReader<R>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<W> AsyncWriteExt for BufReader<R>