pub struct BetterBufReader<R: Read> { /* private fields */ }
Expand description

An implementation of BetterBufRead that wraps a generic Read.

Use this to wrap things like files and network streams, but not data that’s already in memory. This endows the Read with a buffer, unlocking a few benefits:

  • better performance for repeated small reads
  • doesn’t lose data when passed around by programs that optimistically read ahead

Implementations§

source§

impl<R: Read> BetterBufReader<R>

source

pub fn new(preloaded_data: &[u8], inner: R, capacity: usize) -> Self

Creates a BetterBufReader based on a Read.

Providing preloaded data is optional, but can be useful if instantiating based on another abstraction that held a buffer and Read.

Panics if preloaded_data is longer than capacity.

source

pub fn from_read_simple(inner: R) -> Self

Creates a BetterBufReader based on a Reader, supplying sensible defaults.

source

pub fn from_buf_reader(br: BufReader<R>, capacity: usize) -> Self

Creates a BetterBufReader based on a BufReader.

Panics if the BufReader’s buffer is longer than capacity.

source

pub fn into_inner(self) -> R

Returns the inner Read, dropping the BetterBufReader and its buffer.

To avoid losing data, be sure to read the last of the buffer before calling this.

Trait Implementations§

source§

impl<R: Read> BetterBufRead for BetterBufReader<R>

source§

fn fill_or_eof(&mut self, n_bytes: usize) -> Result<()>

Fills the internal buffer with at least n_bytes if possible, or as many as possible if the end of the file is reached. Read more
source§

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

Returns all data available in memory. Read more
source§

fn consume(&mut self, n_bytes: usize)

Advances by n_bytes, reducing the size of the available data to read. Read more
source§

fn capacity(&self) -> Option<usize>

Returns the capacity of the internal buffer, if one exists. Read more
source§

fn resize_capacity(&mut self, desired: usize)

Modifies capacity of the internal buffer, if one exists. Read more

Auto Trait Implementations§

§

impl<R> Freeze for BetterBufReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for BetterBufReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for BetterBufReader<R>
where R: Send,

§

impl<R> Sync for BetterBufReader<R>
where R: Sync,

§

impl<R> Unpin for BetterBufReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for BetterBufReader<R>
where R: UnwindSafe,

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>,

§

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>,

§

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.