BuffedRead

Trait BuffedRead 

Source
pub trait BuffedRead<Alloc = Global>
where Alloc: Allocator + Default,
{ type Data: FromBytes + ?Sized; // Required methods fn data(&self) -> &Self::Data; fn reallocate(&mut self, capacity: usize) -> Result<(), Alloc>; fn require_fill_buf_no_alloc( &mut self, amount: usize, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>; fn consume( &mut self, amount: usize, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>; // Provided methods fn require_fill_buf( &mut self, amount: usize, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>> { ... } fn fill_buf( &mut self, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>> { ... } }
Expand description

A more powerful version of std::io::BufRead, which allows parsing simple types and control buffering and reallocations.

A straightforward example is reading string directly through this trait’s low-level API (while std::io::BufRead only allows reading bytes, even though it provides higher-level functions like read_line):

let s: &str = buffed_read.fill_buf().unwrap();

Required Associated Types§

Source

type Data: FromBytes + ?Sized

The type parsed by the BuffedRead.

Required Methods§

Source

fn data(&self) -> &Self::Data

Returns a reference to the internal buffer.

This is equivalent to calling require_fill_buf_no_alloc (&mut self, 0), without an exclusive borrow.

Source

fn reallocate(&mut self, capacity: usize) -> Result<(), Alloc>

Re-allocate a new buffer for the BuffedReader with a new capacity.

The BuffedRead implementation may allocate more than capacity.

§Error

If the allocation fails (e.g. if there’s not enough memory left in the system), an error is returned.

Source

fn require_fill_buf_no_alloc( &mut self, amount: usize, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>

Returns the content of the internal buffer, filling it with more data from the underlying reader if it contains less than amount bytes.

This function may not reallocate the underlying buffer. If not enough space is available, it will return an error.

§Returns

The returned slice will have at least amount bytes unless EOF has been reached. In which case the returned slice will contain all the data left.

If repeatedly called, this function will return the same result unless consume is called.

§Error

This method will return an Error if any of the following happen:

  • Invalid data was read, and less than amount bytes of valid data are available.
  • An std::io::Error is encountered.
  • The buffer isn’t big enough for the required amount of data to be returned.

This method will never return the ConsumedTooMuch or InvalidConsumeAmount variants of Error.

§Examples
loop {
  let data: &str = buffed_read.fill_buf().unwrap();
  if data.is_empty() {
    break; // Reached EOF
  }
  let size = data.size();
  // Do something with data ...
  buffed_read.consume(size).unwrap();
}
Source

fn consume( &mut self, amount: usize, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>

Consume an amount of bytes of data, which will not be returned anymore by BuffedRead::fill_buf.

This method may not reallocate.

§Returns

The content of the buffer without the just-consumed data.

§Error

If amount is bigger than the amount of data there is to consume, an Error::ConsumedTooMuch is returned.

If consuming the given amount of bytes would leave the buffer in an invalid state, an Error::InvalidConsumeAmount is returned.

If an error is returned, no data is consumed and the BuffedRead’s state is left the same as before the call.

Provided Methods§

Source

fn require_fill_buf( &mut self, amount: usize, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>

Like require_fill_buf_no_alloc, but is allowed to reallocate if the underlying buffer isn’t large enough.

§Returns

The underlying buffer’s content, like require_fill_buf_no_alloc.

§Error

In addition to all error cases of require_fill_buf_no_alloc, an Error::Alloc is returned if allocating failed.

Source

fn fill_buf( &mut self, ) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>

Like require_fill_buf, but doesn’t require any particular amount of bytes, just that some data may be filled in the underlying buffer.

This method is equivalent to std::io::BufRead::fill_buf.

Implementors§

Source§

impl<A, R, D, B> Read<A> for Reader<R, D, B, A>
where A: Allocator + Default, R: Read + Debug, D: FromBytes + ?Sized, B: Buffer<A, Data = D> + Debug,

Available on crate feature std only.
Source§

type Data = <B as Buffer<A>>::Data