DynReadBuffer

Struct DynReadBuffer 

Source
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>

Source

pub fn new(reader: R) -> Self

Creates a new DynReadBuffer to read from the given Read.

Source

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.

Source

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]);
Source

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]);

Auto Trait Implementations§

§

impl<R> Freeze for DynReadBuffer<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for DynReadBuffer<R>
where R: RefUnwindSafe,

§

impl<R> Send for DynReadBuffer<R>
where R: Send,

§

impl<R> Sync for DynReadBuffer<R>
where R: Sync,

§

impl<R> Unpin for DynReadBuffer<R>
where R: Unpin,

§

impl<R> UnwindSafe for DynReadBuffer<R>
where 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>,

Source§

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>,

Source§

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.