pub struct BufReader<R, P = StdPolicy> { /* private fields */ }
Expand description
A drop-in replacement for std::io::BufReader
with more functionality.
Original method names/signatures and implemented traits are left untouched, making replacement as simple as swapping the import of the type.
By default this type implements the behavior of its std
counterpart: it only reads into
the buffer when it is empty.
To change this type’s behavior, change the policy with .set_policy()
using a type
from the policy
module or your own implementation of ReaderPolicy
.
Policies that perform alternating reads and consumes without completely emptying the buffer
may benefit from using a ringbuffer via the new_ringbuf()
and with_capacity_ringbuf()
constructors. Ringbuffers are only available on supported platforms with the
slice-deque
feature and have some other caveats; see the crate root docs
for more details.
Implementations§
Source§impl<R> BufReader<R>
impl<R> BufReader<R>
Sourcepub fn new(inner: R) -> BufReader<R> ⓘ
pub fn new(inner: R) -> BufReader<R> ⓘ
Create a new BufReader
wrapping inner
, utilizing a buffer of
default capacity and the default ReaderPolicy
.
Sourcepub fn with_capacity(cap: usize, inner: R) -> BufReader<R> ⓘ
pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> ⓘ
Create a new BufReader
wrapping inner
, utilizing a buffer with a capacity
of at least cap
bytes and the default ReaderPolicy
.
The actual capacity of the buffer may vary based on implementation details of the global allocator.
Source§impl<R, P> BufReader<R, P>
impl<R, P> BufReader<R, P>
Sourcepub fn set_policy<P_>(self, policy: P_) -> BufReader<R, P_> ⓘwhere
P_: ReaderPolicy,
pub fn set_policy<P_>(self, policy: P_) -> BufReader<R, P_> ⓘwhere
P_: ReaderPolicy,
Apply a new ReaderPolicy
to this BufReader
, returning the transformed type.
Sourcepub fn policy_mut(&mut self) -> &mut P
pub fn policy_mut(&mut self) -> &mut P
Mutate the current ReaderPolicy
in-place.
If you want to change the type, use .set_policy()
.
Sourcepub fn make_room(&mut self)
pub fn make_room(&mut self)
Move data to the start of the buffer, making room at the end for more reading.
This is a no-op with the *_ringbuf()
constructors (requires slice-deque
feature).
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensure room in the buffer for at least additional
bytes. May not be
quite exact due to implementation details of the buffer’s allocator.
Sourcepub fn buffer(&self) -> &[u8] ⓘ
pub fn buffer(&self) -> &[u8] ⓘ
Get the section of the buffer containing valid data; may be empty.
Call .consume()
to remove bytes from the beginning of this section.
Sourcepub fn get_mut(&mut self) -> &mut R
pub fn get_mut(&mut self) -> &mut R
Get a mutable reference to the underlying reader.
§Note
Reading directly from the underlying reader is not recommended, as some data has likely already been moved into the buffer.
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consume self
and return the inner reader only.
Sourcepub fn into_inner_with_buffer(self) -> (R, Buffer)
pub fn into_inner_with_buffer(self) -> (R, Buffer)
Consume self
and return both the underlying reader and the buffer.
See also: BufReader::unbuffer()
Trait Implementations§
Source§impl<R, P> BufRead for BufReader<R, P>where
R: Read,
P: ReaderPolicy,
impl<R, P> BufRead for BufReader<R, P>where
R: Read,
P: ReaderPolicy,
Source§fn fill_buf(&mut self) -> Result<&[u8], Error>
fn fill_buf(&mut self) -> Result<&[u8], Error>
Source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte
or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moreSource§impl<R, P> Read for BufReader<R, P>where
R: Read,
P: ReaderPolicy,
impl<R, P> Read for BufReader<R, P>where
R: Read,
P: ReaderPolicy,
Source§fn read(&mut self, out: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, out: &mut [u8]) -> Result<usize, Error>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<R, P> Seek for BufReader<R, P>where
R: Seek,
P: ReaderPolicy,
impl<R, P> Seek for BufReader<R, P>where
R: Seek,
P: ReaderPolicy,
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
Seek to an ofPet, 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
.unwrap()
immediately after a seek yields the underlying reader at
the same position.
See std::io::Seek
for more details.
Note: In the edge case where you’re seeking with SeekFrom::Current(n)
where n
minus the internal buffer length underflows 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 seeked to SeekFrom::Current(0)
.
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)Auto Trait Implementations§
impl<R, P> Freeze for BufReader<R, P>
impl<R, P> RefUnwindSafe for BufReader<R, P>where
R: RefUnwindSafe,
P: RefUnwindSafe,
impl<R, P> Send for BufReader<R, P>
impl<R, P> Sync for BufReader<R, P>
impl<R, P> Unpin for BufReader<R, P>
impl<R, P> UnwindSafe for BufReader<R, P>where
R: UnwindSafe,
P: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R> ReadBytesExt for R
impl<R> ReadBytesExt for R
Source§fn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
Source§fn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
Source§fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
Source§fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
Source§fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
Source§fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
Source§fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
Source§fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
read_f32_into
instead