Struct BufReader

Source
pub struct BufReader<'buf, R> { /* private fields */ }
Available on crate feature io only.
Expand description

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§

Source§

impl<'buf, R: AsyncRead> BufReader<'buf, R>

Source

pub fn new(buf: &'buf mut [u8], inner: R) -> Self

Creates a new BufReader with the specified buffer capacity.

Source

pub fn get_ref(&self) -> &R

Gets a reference to the underlying reader.

It is inadvisable to directly read from the underlying reader.

Source

pub fn get_mut(&mut self) -> &mut R

Gets a mutable reference to the underlying reader.

It is inadvisable to directly read from the underlying reader.

Source

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

Gets a pinned mutable reference to the underlying reader.

It is inadvisable to directly read from the underlying reader.

Source

pub fn into_inner(self) -> R

Consumes this BufReader, returning the underlying reader.

Note that any leftover data in the internal buffer is lost.

Source

pub fn buffer(&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.

Trait Implementations§

Source§

impl<R: AsyncRead> AsyncBufRead for BufReader<'_, R>

Source§

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

Attempts to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Source§

fn consume(self: Pin<&mut Self>, amt: usize)

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more
Source§

impl<R: AsyncRead> AsyncRead for BufReader<'_, R>

Source§

type Error = <R as AsyncRead>::Error

Source§

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

Attempt to read from the AsyncRead into buf. On success, returns Poll::Ready(Ok(num_bytes_read)). If no data is available for reading, this method returns Poll::Pending and arranges for the current task to be woken.
Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where Self: Unpin,

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
Source§

impl<'__pin, 'buf, R> Unpin for BufReader<'buf, R>
where PinnedFieldsOf<__Origin<'__pin, 'buf, R>>: Unpin,

Auto Trait Implementations§

§

impl<'buf, R> Freeze for BufReader<'buf, R>
where R: Freeze,

§

impl<'buf, R> RefUnwindSafe for BufReader<'buf, R>
where R: RefUnwindSafe,

§

impl<'buf, R> Send for BufReader<'buf, R>
where R: Send,

§

impl<'buf, R> Sync for BufReader<'buf, R>
where R: Sync,

§

impl<'buf, R> !UnwindSafe for BufReader<'buf, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.