pub trait BuffedRead<Alloc = Global>{
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§
Required Methods§
Sourcefn data(&self) -> &Self::Data
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.
Sourcefn reallocate(&mut self, capacity: usize) -> Result<(), Alloc>
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.
Sourcefn require_fill_buf_no_alloc(
&mut self,
amount: usize,
) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>
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
amountbytes of valid data are available. - An
std::io::Erroris encountered. - The buffer isn’t big enough for the required
amountof 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();
}Sourcefn consume(
&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>>
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§
Sourcefn require_fill_buf(
&mut self,
amount: usize,
) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>
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.
Sourcefn fill_buf(
&mut self,
) -> Result<&Self::Data, Error<<Self::Data as FromBytes>::Error>>
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.