pub trait ByteReader {
Show 16 methods
// Required methods
fn read_u8(&mut self) -> Result<u8, DeserializationError>;
fn peek_u8(&self) -> Result<u8, DeserializationError>;
fn read_slice(&mut self, len: usize) -> Result<&[u8], DeserializationError>;
fn read_array<const N: usize>(
&mut self,
) -> Result<[u8; N], DeserializationError>;
fn check_eor(&self, num_bytes: usize) -> Result<(), DeserializationError>;
fn has_more_bytes(&self) -> bool;
// Provided methods
fn read_bool(&mut self) -> Result<bool, DeserializationError> { ... }
fn read_u16(&mut self) -> Result<u16, DeserializationError> { ... }
fn read_u32(&mut self) -> Result<u32, DeserializationError> { ... }
fn read_u64(&mut self) -> Result<u64, DeserializationError> { ... }
fn read_u128(&mut self) -> Result<u128, DeserializationError> { ... }
fn read_usize(&mut self) -> Result<usize, DeserializationError> { ... }
fn read_vec(&mut self, len: usize) -> Result<Vec<u8>, DeserializationError> { ... }
fn read_string(
&mut self,
num_bytes: usize,
) -> Result<String, DeserializationError> { ... }
fn read<D>(&mut self) -> Result<D, DeserializationError>
where Self: Sized,
D: Deserializable { ... }
fn read_many<D>(
&mut self,
num_elements: usize,
) -> Result<Vec<D>, DeserializationError>
where Self: Sized,
D: Deserializable { ... }
}
Expand description
Defines how primitive values are to be read from Self
.
Whenever data is read from the reader using any of the read_*
functions, the reader advances
to the next unread byte. If the error occurs, the reader is not rolled back to the state prior
to calling any of the function.
Required Methods§
sourcefn read_u8(&mut self) -> Result<u8, DeserializationError>
fn read_u8(&mut self) -> Result<u8, DeserializationError>
Returns a single byte read from self
.
§Errors
Returns a DeserializationError error the reader is at EOF.
sourcefn peek_u8(&self) -> Result<u8, DeserializationError>
fn peek_u8(&self) -> Result<u8, DeserializationError>
Returns the next byte to be read from self
without advancing the reader to the next byte.
§Errors
Returns a DeserializationError error the reader is at EOF.
sourcefn read_slice(&mut self, len: usize) -> Result<&[u8], DeserializationError>
fn read_slice(&mut self, len: usize) -> Result<&[u8], DeserializationError>
Returns a slice of bytes of the specified length read from self
.
§Errors
Returns a DeserializationError if a slice of the specified length could not be read
from self
.
sourcefn read_array<const N: usize>(
&mut self,
) -> Result<[u8; N], DeserializationError>
fn read_array<const N: usize>( &mut self, ) -> Result<[u8; N], DeserializationError>
Returns a byte array of length N
read from self
.
§Errors
Returns a DeserializationError if an array of the specified length could not be read
from self
.
sourcefn check_eor(&self, num_bytes: usize) -> Result<(), DeserializationError>
fn check_eor(&self, num_bytes: usize) -> Result<(), DeserializationError>
Checks if it is possible to read at least num_bytes
bytes from this ByteReader
§Errors
Returns an error if, when reading the requested number of bytes, we go beyond the the data available in the reader.
sourcefn has_more_bytes(&self) -> bool
fn has_more_bytes(&self) -> bool
Returns true if there are more bytes left to be read from self
.
Provided Methods§
sourcefn read_bool(&mut self) -> Result<bool, DeserializationError>
fn read_bool(&mut self) -> Result<bool, DeserializationError>
Returns a boolean value read from self
consuming 1 byte from the reader.
§Errors
Returns a DeserializationError if a u16 value could not be read from self
.
sourcefn read_u16(&mut self) -> Result<u16, DeserializationError>
fn read_u16(&mut self) -> Result<u16, DeserializationError>
Returns a u16 value read from self
in little-endian byte order.
§Errors
Returns a DeserializationError if a u16 value could not be read from self
.
sourcefn read_u32(&mut self) -> Result<u32, DeserializationError>
fn read_u32(&mut self) -> Result<u32, DeserializationError>
Returns a u32 value read from self
in little-endian byte order.
§Errors
Returns a DeserializationError if a u32 value could not be read from self
.
sourcefn read_u64(&mut self) -> Result<u64, DeserializationError>
fn read_u64(&mut self) -> Result<u64, DeserializationError>
Returns a u64 value read from self
in little-endian byte order.
§Errors
Returns a DeserializationError if a u64 value could not be read from self
.
sourcefn read_u128(&mut self) -> Result<u128, DeserializationError>
fn read_u128(&mut self) -> Result<u128, DeserializationError>
Returns a u128 value read from self
in little-endian byte order.
§Errors
Returns a DeserializationError if a u128 value could not be read from self
.
sourcefn read_usize(&mut self) -> Result<usize, DeserializationError>
fn read_usize(&mut self) -> Result<usize, DeserializationError>
Returns a usize value read from self
in vint64
format.
§Errors
Returns a DeserializationError if:
- usize value could not be read from
self
. - encoded value is greater than
usize
maximum value on a given platform.
sourcefn read_vec(&mut self, len: usize) -> Result<Vec<u8>, DeserializationError>
fn read_vec(&mut self, len: usize) -> Result<Vec<u8>, DeserializationError>
Returns a byte vector of the specified length read from self
.
§Errors
Returns a DeserializationError if a vector of the specified length could not be read
from self
.
sourcefn read_string(
&mut self,
num_bytes: usize,
) -> Result<String, DeserializationError>
fn read_string( &mut self, num_bytes: usize, ) -> Result<String, DeserializationError>
Returns a String of the specified length read from self
.
§Errors
Returns a DeserializationError if a String of the specified length could not be read
from self
.
sourcefn read<D>(&mut self) -> Result<D, DeserializationError>where
Self: Sized,
D: Deserializable,
fn read<D>(&mut self) -> Result<D, DeserializationError>where
Self: Sized,
D: Deserializable,
Reads a deserializable value from self
.
§Errors
Returns a DeserializationError if the specified value could not be read from self
.
sourcefn read_many<D>(
&mut self,
num_elements: usize,
) -> Result<Vec<D>, DeserializationError>where
Self: Sized,
D: Deserializable,
fn read_many<D>(
&mut self,
num_elements: usize,
) -> Result<Vec<D>, DeserializationError>where
Self: Sized,
D: Deserializable,
Reads a sequence of bytes from self
, attempts to deserialize these bytes into a vector
with the specified number of D
elements, and returns the result.
§Errors
Returns a DeserializationError if the specified number elements could not be read from
self
.