Struct better_io::BetterBufReader
source · 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>
impl<R: Read> BetterBufReader<R>
sourcepub fn new(preloaded_data: &[u8], inner: R, capacity: usize) -> Self
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
.
sourcepub fn from_read_simple(inner: R) -> Self
pub fn from_read_simple(inner: R) -> Self
Creates a BetterBufReader
based on a Reader
, supplying sensible
defaults.
sourcepub fn from_buf_reader(br: BufReader<R>, capacity: usize) -> Self
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
.
sourcepub fn into_inner(self) -> R
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>
impl<R: Read> BetterBufRead for BetterBufReader<R>
source§fn fill_or_eof(&mut self, n_bytes: usize) -> Result<()>
fn fill_or_eof(&mut self, n_bytes: usize) -> Result<()>
n_bytes
if possible, or as many
as possible if the end of the file is reached. Read moresource§fn consume(&mut self, n_bytes: usize)
fn consume(&mut self, n_bytes: usize)
n_bytes
, reducing the size of the available data to read. Read more