pub struct BufferedInput<I>{ /* private fields */ }Expand description
Buffered unit input over a wrapped input source.
This type owns a wrapped input object and an internal unit buffer. It keeps
unread units in buffer[position..limit] so callers can inspect or consume
the current unit window before refilling it.
BufferedInput is deliberately unit-oriented. It performs no binary
decoding, text decoding, or record parsing; higher-level stream adapters can
build those concerns on top of Self::unread_slice,
Self::unread_raw_parts, Self::ensure_available, and
Self::read_into_unchecked. The type also implements BufRead for
callers that want the standard buffered-read interface.
Implementations§
Source§impl<I> BufferedInput<I>
impl<I> BufferedInput<I>
Sourcepub fn with_capacity(inner: I, capacity: usize) -> Self
pub fn with_capacity(inner: I, capacity: usize) -> Self
Creates a buffered unit input with at least the requested capacity.
The actual capacity is raised to 1 when the requested value is 0.
§Arguments
inner- The input object wrapped by this buffer.capacity- The requested internal buffer capacity, in units.
§Returns
A new buffered unit input whose internal buffer capacity is
capacity.max(1).
Sourcepub const fn inner(&self) -> &I
pub const fn inner(&self) -> &I
Returns a shared reference to the wrapped input object.
§Returns
A shared reference to the inner input object.
Sourcepub fn inner_mut(&mut self) -> &mut I
pub fn inner_mut(&mut self) -> &mut I
Returns an exclusive reference to the wrapped input object.
Mutating the wrapped object directly may invalidate assumptions about units already buffered by this value.
§Returns
An exclusive reference to the wrapped input object.
Sourcepub fn into_parts(self) -> (I, Vec<I::Item>)
pub fn into_parts(self) -> (I, Vec<I::Item>)
Consumes this buffered input and returns the wrapped input object plus unread bytes.
This method performs no I/O. Units that have already been read from the wrapped input but not consumed by this buffered input are returned as the second tuple item.
§Returns
The wrapped input object and a vector containing the unread buffered units in logical read order.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the internal buffer capacity.
§Returns
The total number of units that can be held by the internal buffer.
Sourcepub fn available(&self) -> usize
pub fn available(&self) -> usize
Returns the number of unread units currently buffered.
§Returns
The length of buffer[position..limit], in units.
Sourcepub fn unread_slice(&self) -> &[I::Item]
pub fn unread_slice(&self) -> &[I::Item]
Sourcepub fn unread_raw_parts(&self) -> (&[I::Item], usize, usize)
pub fn unread_raw_parts(&self) -> (&[I::Item], usize, usize)
Returns raw unread-buffer parts for hot-path callers.
The returned slice is the full internal backing storage. index is the
start of the unread unit window, and count is the number of unread
units. Callers that need a slice can use &buffer[index..index + count]; callers that already validated bounds can pass buffer and
index directly to indexed unchecked codecs.
§Returns
The backing storage, the unread start index, and the unread unit count.
Sourcepub fn consume(&mut self, count: usize)
pub fn consume(&mut self, count: usize)
Advances the unread cursor by count units.
§Parameters
count- Number of currently unread units to consume.
§Panics
Panics when count exceeds Self::available.
Sourcepub unsafe fn consume_unchecked(&mut self, count: usize)
pub unsafe fn consume_unchecked(&mut self, count: usize)
Source§impl<I> BufferedInput<I>
impl<I> BufferedInput<I>
Sourcepub fn fill_more(&mut self) -> Result<bool>
pub fn fill_more(&mut self) -> Result<bool>
Refills the internal buffer after preserving unread bytes.
Consumed bytes may be discarded, and unread bytes may be moved to the front of the buffer before the wrapped reader is called.
§Returns
Ok(true) if at least one byte was appended, or Ok(false) at EOF.
§Errors
Returns any non-interrupted I/O error produced by the wrapped reader.
Sourcepub fn fill_until(&mut self, count: usize) -> Result<bool>
pub fn fill_until(&mut self, count: usize) -> Result<bool>
Refills the buffer until at least count unread bytes are available.
This method may discard consumed bytes or move unread bytes to the front
of the buffer before reading more data. It stops as soon as the unread
window reaches count bytes or the wrapped reader reaches EOF.
§Parameters
count- Minimum number of unread bytes required.
§Returns
Ok(true) if at least count unread bytes are buffered. Ok(false)
means EOF was reached before the requested byte count became available.
§Errors
Returns ErrorKind::InvalidInput when count exceeds the internal
buffer capacity. Returns ErrorKind::InvalidData if the wrapped
reader reports more bytes than the spare buffer range could hold.
Returns any non-interrupted I/O error produced by the wrapped reader
while refilling the buffer.
Sourcepub fn ensure_available(&mut self, count: usize) -> Result<()>
pub fn ensure_available(&mut self, count: usize) -> Result<()>
Ensures that at least count unread bytes are available.
Unlike Self::fill_until, this method treats EOF before the requested
byte count as ErrorKind::UnexpectedEof. Any partial bytes buffered
before EOF are consumed so callers observe the same logical position as
a failed exact read.
§Parameters
count- Minimum number of unread bytes required.
§Errors
Returns ErrorKind::UnexpectedEof if EOF is reached before count
bytes are available. Returns ErrorKind::InvalidInput when count
exceeds the internal buffer capacity. Returns ErrorKind::InvalidData
if the wrapped reader reports more bytes than the spare buffer range
could hold. Returns any non-interrupted I/O error produced by the
wrapped reader while refilling the buffer.
Sourcepub unsafe fn read_into_unchecked(
&mut self,
output: &mut [I::Item],
output_index: usize,
count: usize,
) -> Result<usize>
pub unsafe fn read_into_unchecked( &mut self, output: &mut [I::Item], output_index: usize, count: usize, ) -> Result<usize>
Reads bytes through the internal buffer into an indexed output range.
If the internal buffer is empty and count is at least as large as the
internal buffer capacity, the read is delegated directly to the wrapped
reader to avoid an unnecessary copy. Otherwise, bytes are served from
the internal buffer.
§Arguments
output- Destination storage that receives bytes.output_index- Start index insideoutput.count- Maximum number of bytes to read.
§Returns
The number of bytes written into output[output_index..output_index + count]. A return value of 0 means that count was zero or EOF was
reached before any bytes were read.
§Errors
Returns any I/O error produced by the wrapped reader. Returns
ErrorKind::InvalidData if the wrapped reader reports more bytes
than the requested destination range could hold. Interrupted reads are
retried when the method refills the internal buffer through
read_more; direct delegated reads follow the wrapped reader’s own
Read::read behavior.
§Safety
The caller must guarantee that output_index..output_index + count is
a valid range inside output and that the addition does not overflow.
Trait Implementations§
Source§impl<I> BufRead for BufferedInput<I>
impl<I> BufRead for BufferedInput<I>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Returns the currently buffered unread bytes, refilling when empty.
Source§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left)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<I> Debug for BufferedInput<I>
impl<I> Debug for BufferedInput<I>
Source§impl<I> Read for BufferedInput<I>
impl<I> Read for BufferedInput<I>
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 more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl<I> Seek for BufferedInput<I>
impl<I> Seek for BufferedInput<I>
Source§fn seek(&mut self, position: SeekFrom) -> Result<u64>
fn seek(&mut self, position: SeekFrom) -> Result<u64>
Seeks the wrapped reader and discards buffered bytes after success.
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<I> Freeze for BufferedInput<I>
impl<I> RefUnwindSafe for BufferedInput<I>
impl<I> Send for BufferedInput<I>
impl<I> Sync for BufferedInput<I>
impl<I> Unpin for BufferedInput<I>
impl<I> UnsafeUnpin for BufferedInput<I>
impl<I> UnwindSafe for BufferedInput<I>
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<T> BufReadExt for T
impl<T> BufReadExt for T
Source§fn read_until_limited_into(
&mut self,
delimiter: u8,
output: &mut Vec<u8>,
max_len: usize,
) -> Result<usize, Error>
fn read_until_limited_into( &mut self, delimiter: u8, output: &mut Vec<u8>, max_len: usize, ) -> Result<usize, Error>
Source§fn read_line_limited(&mut self, max_len: usize) -> Result<String, Error>
fn read_line_limited(&mut self, max_len: usize) -> Result<String, Error>
max_len. Read moreimpl<T> BufReadSeek for T
Source§impl<T> ReadExt for T
impl<T> ReadExt for T
Source§unsafe fn read_unchecked(
&mut self,
buffer: &mut [u8],
start_index: usize,
count: usize,
) -> Result<usize, Error>
unsafe fn read_unchecked( &mut self, buffer: &mut [u8], start_index: usize, count: usize, ) -> Result<usize, Error>
buffer without checking the range bounds
in release builds. Read moreSource§unsafe fn read_exact_or_eof_unchecked(
&mut self,
buffer: &mut [u8],
start_index: usize,
count: usize,
) -> Result<usize, Error>
unsafe fn read_exact_or_eof_unchecked( &mut self, buffer: &mut [u8], start_index: usize, count: usize, ) -> Result<usize, Error>
buffer until that range is full or EOF is
reached, without checking the range bounds in release builds. Read moreSource§unsafe fn read_exact_unchecked(
&mut self,
buffer: &mut [u8],
start_index: usize,
count: usize,
) -> Result<(), Error>
unsafe fn read_exact_unchecked( &mut self, buffer: &mut [u8], start_index: usize, count: usize, ) -> Result<(), Error>
count bytes into a range of buffer without checking
the range bounds in release builds. Read moreSource§fn read_exact_or_eof(&mut self, buffer: &mut [u8]) -> Result<usize, Error>
fn read_exact_or_eof(&mut self, buffer: &mut [u8]) -> Result<usize, Error>
buffer is full or EOF is reached. Read moreSource§fn read_exact_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
fn read_exact_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
N bytes into a stack-allocated array. Read moreSource§fn read_exact_vec_limited(
&mut self,
len: usize,
max_len: usize,
) -> Result<Vec<u8>, Error>
fn read_exact_vec_limited( &mut self, len: usize, max_len: usize, ) -> Result<Vec<u8>, Error>
len bytes into a new vector after checking a limit. Read moreSource§fn read_exact_vec_limited_into(
&mut self,
output: &mut Vec<u8>,
len: usize,
max_len: usize,
) -> Result<(), Error>
fn read_exact_vec_limited_into( &mut self, output: &mut Vec<u8>, len: usize, max_len: usize, ) -> Result<(), Error>
Source§fn discard_exact_or_eof(&mut self, bytes: u64) -> Result<u64, Error>
fn discard_exact_or_eof(&mut self, bytes: u64) -> Result<u64, Error>
bytes bytes from this reader. Read moreSource§fn copy_to(&mut self, writer: &mut dyn Write) -> Result<u64, Error>
fn copy_to(&mut self, writer: &mut dyn Write) -> Result<u64, Error>
writer. Read moreSource§fn copy_to_at_most(
&mut self,
writer: &mut dyn Write,
max_bytes: u64,
) -> Result<u64, Error>
fn copy_to_at_most( &mut self, writer: &mut dyn Write, max_bytes: u64, ) -> Result<u64, Error>
Source§fn copy_to_end_limited(
&mut self,
writer: &mut dyn Write,
max_bytes: u64,
) -> Result<u64, Error>
fn copy_to_end_limited( &mut self, writer: &mut dyn Write, max_bytes: u64, ) -> Result<u64, Error>
max_bytes. Read moreSource§fn read_to_end_limited(&mut self, max_len: usize) -> Result<Vec<u8>, Error>
fn read_to_end_limited(&mut self, max_len: usize) -> Result<Vec<u8>, Error>
Source§fn read_to_end_limited_into(
&mut self,
output: &mut Vec<u8>,
max_len: usize,
) -> Result<usize, Error>
fn read_to_end_limited_into( &mut self, output: &mut Vec<u8>, max_len: usize, ) -> Result<usize, Error>
output with a maximum accepted length. Read more