pub struct DynReadBuffer<R: Read> { /* private fields */ }Expand description
A dynamically sized buffer to read into from a Read and safely access the read data.
DynReadBuffer provides a heap-allocated buffer to read into using
read_bytes or read_until,
but crucially doesn’t allow any access to the data inside the buffer
outside of the slices returned from read_bytes and read_until.
This type is preferrable over ReadBuffer when the maximum expected size of a single read
is not known at compile time.
Implementations§
Source§impl<R: Read> DynReadBuffer<R>
impl<R: Read> DynReadBuffer<R>
Sourcepub fn with_capacity(reader: R, capacity: usize) -> Self
pub fn with_capacity(reader: R, capacity: usize) -> Self
Creates a new DynReadBuffer to read from the given Read with an internal buffer of at least the specified capacity.
Sourcepub fn read_bytes(&mut self, amount: usize) -> Result<&[u8], Error>
pub fn read_bytes(&mut self, amount: usize) -> Result<&[u8], Error>
Reads the specified amount of bytes from the given Read into the internal buffer and returns a slice referencing the read data.
§Errors
If the given Read reaches its “end of file” before the requested amount of bytes could be read, an error of the kind ErrorKind::UnexpectedEof is returned following the behavior of Read::read_exact.
If an error of the kind ErrorKind::Interrupted is encountered, it is ignored.
All other errors from Read::read_exact are passed on to the caller.
§Examples
use read_buffer::DynReadBuffer;
let mut reader = [1, 2, 3, 4].as_slice(); // Read is implemented for &[u8]
let mut buffer = DynReadBuffer::new(reader);
let read_data = buffer.read_bytes(3)?;
assert_eq!(read_data, [1, 2, 3]);Sourcepub fn read_until(&mut self, delimiter: u8) -> Result<&[u8], Error>
pub fn read_until(&mut self, delimiter: u8) -> Result<&[u8], Error>
Reads from the given Read until the specified delimiter is encountered and returns a slice referencing the data up to and including the delimiter.
§Errors
If any error occurs, the data read so far is preserved in the internal buffer for future reads.
If the given Read reaches its “end of file” before the delimiter was encountered, an error of the kind ErrorKind::UnexpectedEof is returned.
If an error of the kind ErrorKind::Interrupted is encountered, it is ignored.
All other errors from Read::read are passed on to the caller.
§Examples
use read_buffer::DynReadBuffer;
let mut reader = [1, 2, 3, 0, 4].as_slice();
let mut buffer = DynReadBuffer::new(reader);
let read_data = buffer.read_until(0)?;
assert_eq!(read_data, [1, 2, 3, 0]);
assert_eq!(buffer.read_bytes(1)?, [4]);