pub struct Byte(pub u8);
Expand description
Wrapper for a byte of information. It differs from an u8
on how it is deserialized.
A Byte
reads exactly one byte from the input iterator, while a u8
could potentially read
several bytes (since unsigned integers are encoded using LEB-128).
A Byte
takes exactly one byte of memory, and can be transmuted to u8
.
This is specially useful since it allows transmuting a vector of Byte
s to a vector of u8
.
§Example
let mut iter = [0x8E].iter().copied();
let Byte(result) = deserialize(&mut iter).unwrap();
assert_eq!(result, 0x8E);
Deserialization will return an error if the iterator is exhausted while deserializing.
let mut iter = [0x00].iter().copied();
let Byte(_) = deserialize(&mut iter).unwrap();
let result: Result<Byte> = deserialize(&mut iter);
assert!(result.is_err());
Tuple Fields§
§0: u8
Trait Implementations§
Source§impl Grammar for Byte
impl Grammar for Byte
Source§fn deserialize<Iter: Iterator<Item = u8>>(iter: &mut Iter) -> Result<Self>
fn deserialize<Iter: Iterator<Item = u8>>(iter: &mut Iter) -> Result<Self>
Read a Byte
from an Iterator<u8>
. It returns Ok(Byte(_))
on success, and
Error(Error::UnexpectedEndOfStream)
if the iterator returns None
.
Deserializing a Byte
instead of using iter.next()
is an easy way to convert the
Option<u8>
returned by the iterator into a Result<u8>
with a meaningful error type.