Struct buffer_redux::BufReader

source ·
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, StdPolicy>

source

pub fn new(inner: R) -> Self

Create a new BufReader wrapping inner, utilizing a buffer of default capacity and the default ReaderPolicy.

source

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

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

pub fn new_ringbuf(inner: R) -> Self

Create a new BufReader wrapping inner, utilizing a ringbuffer with the default capacity and ReaderPolicy.

A ringbuffer never has to move data to make room; consuming bytes from the head simultaneously makes room at the tail. This is useful in conjunction with a policy like MinBuffered to ensure there is always room to read more data if necessary, without expensive copying operations.

Only available on platforms with virtual memory support and with the slice-deque feature enabled. The default capacity will differ between Windows and Unix-derivative targets. See Buffer::new_ringbuf() or the crate root docs for more info.

source

pub fn with_capacity_ringbuf(cap: usize, inner: R) -> Self

Create a new BufReader wrapping inner, utilizing a ringbuffer with at least the given capacity and the default ReaderPolicy.

A ringbuffer never has to move data to make room; consuming bytes from the head simultaneously makes room at the tail. This is useful in conjunction with a policy like MinBuffered to ensure there is always room to read more data if necessary, without expensive copying operations.

Only available on platforms with virtual memory support and with the slice-deque feature enabled. The capacity will be rounded up to the minimum size for the target platform. See Buffer::with_capacity_ringbuf() or the crate root docs for more info.

source

pub fn with_buffer(buf: Buffer, inner: R) -> Self

Wrap inner with an existing Buffer instance and the default ReaderPolicy.

§Note

Does not clear the buffer first! If there is data already in the buffer then it will be returned in read() and fill_buf() ahead of any data from inner.

source§

impl<R, P> BufReader<R, P>

source

pub fn set_policy<P_: ReaderPolicy>(self, policy: P_) -> BufReader<R, P_>

Apply a new ReaderPolicy to this BufReader, returning the transformed type.

source

pub fn policy_mut(&mut self) -> &mut P

Mutate the current ReaderPolicy in-place.

If you want to change the type, use .set_policy().

source

pub fn policy(&self) -> &P

Inspect the current ReaderPolicy.

source

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).

source

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.

source

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.

source

pub fn buf_len(&self) -> usize

Get the current number of bytes available in the buffer.

source

pub fn capacity(&self) -> usize

Get the total buffer capacity.

source

pub fn get_ref(&self) -> &R

Get an immutable reference to the underlying reader.

source

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.

source

pub fn into_inner(self) -> R

Consume self and return the inner reader only.

source

pub fn into_inner_with_buffer(self) -> (R, Buffer)

Consume self and return both the underlying reader and the buffer.

See also: BufReader::unbuffer()

source

pub fn unbuffer(self) -> Unbuffer<R>

Consume self and return an adapter which implements Read and will empty the buffer before reading directly from the underlying reader.

source§

impl<R: Read, P> BufReader<R, P>

source

pub fn read_into_buf(&mut self) -> Result<usize>

Unconditionally perform a read into the buffer.

Does not invoke ReaderPolicy methods.

If the read was successful, returns the number of bytes read.

source

pub fn boxed<'a>(self) -> BufReader<Box<dyn Read + 'a>, P>
where R: 'a,

Box the inner reader without losing data.

Trait Implementations§

source§

impl<R: Read, P: ReaderPolicy> BufRead for BufReader<R, P>

source§

fn fill_buf(&mut self) -> Result<&[u8]>

Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
source§

fn consume(&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 read. Read more
source§

fn has_data_left(&mut self) -> Result<bool, Error>

🔬This is a nightly-only experimental API. (buf_read_has_data_left)
Check if the underlying Read has any data left to be read. Read more
1.0.0 · source§

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

Read all bytes into buf until the delimiter byte or EOF is reached. Read more
source§

fn skip_until(&mut self, byte: u8) -> Result<usize, Error>

🔬This is a nightly-only experimental API. (bufread_skip_until)
Skip all bytes until the delimiter byte or EOF is reached. Read more
1.0.0 · source§

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided String buffer. Read more
1.0.0 · source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte. Read more
1.0.0 · source§

fn lines(self) -> Lines<Self>
where Self: Sized,

Returns an iterator over the lines of this reader. Read more
source§

impl<R: Debug, P: Debug> Debug for BufReader<R, P>

source§

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

Formats the value using the given formatter. Read more
source§

impl<R: Read, P: ReaderPolicy> Read for BufReader<R, P>

source§

fn read(&mut self, out: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Read all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl<R: Seek, P: ReaderPolicy> Seek for BufReader<R, P>

source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

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>

Rewind to the beginning of a stream. Read more
source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

🔬This is a nightly-only experimental API. (seek_seek_relative)
Seeks relative to the current position. Read more

Auto Trait Implementations§

§

impl<R, P> RefUnwindSafe for BufReader<R, P>

§

impl<R, P> Send for BufReader<R, P>
where P: Send, R: Send,

§

impl<R, P> Sync for BufReader<R, P>
where P: Sync, R: Sync,

§

impl<R, P> Unpin for BufReader<R, P>
where P: Unpin, R: Unpin,

§

impl<R, P> UnwindSafe for BufReader<R, P>
where P: UnwindSafe, 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.