pub struct Decoder<'a> { /* private fields */ }
Expand description
Decoder reads from a binary slice buffer (&[u8]
) and exposes
methods to read BitSparrow types from it in the same order they
were encoded by the Encoder
.
Implementations§
Source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
Sourcepub fn uint8(&mut self) -> Result<u8, Error>
pub fn uint8(&mut self) -> Result<u8, Error>
Read a u8
from the buffer and progress the internal index.
Sourcepub fn uint16(&mut self) -> Result<u16, Error>
pub fn uint16(&mut self) -> Result<u16, Error>
Read a u16
from the buffer and progress the internal index.
Sourcepub fn uint32(&mut self) -> Result<u32, Error>
pub fn uint32(&mut self) -> Result<u32, Error>
Read a u32
from the buffer and progress the internal index.
Sourcepub fn uint64(&mut self) -> Result<u64, Error>
pub fn uint64(&mut self) -> Result<u64, Error>
Read a u64
from the buffer and progress the internal index.
Sourcepub fn int8(&mut self) -> Result<i8, Error>
pub fn int8(&mut self) -> Result<i8, Error>
Read an i8
from the buffer and progress the internal index.
Sourcepub fn int16(&mut self) -> Result<i16, Error>
pub fn int16(&mut self) -> Result<i16, Error>
Read an i16
from the buffer and progress the internal index.
Sourcepub fn int32(&mut self) -> Result<i32, Error>
pub fn int32(&mut self) -> Result<i32, Error>
Read an i32
from the buffer and progress the internal index.
Sourcepub fn int64(&mut self) -> Result<i64, Error>
pub fn int64(&mut self) -> Result<i64, Error>
Read an i64
from the buffer and progress the internal index.
Sourcepub fn float32(&mut self) -> Result<f32, Error>
pub fn float32(&mut self) -> Result<f32, Error>
Read a float32
from the buffer and progress the internal index.
Sourcepub fn float64(&mut self) -> Result<f64, Error>
pub fn float64(&mut self) -> Result<f64, Error>
Read a float64
from the buffer and progress the internal index.
Sourcepub fn bool(&mut self) -> Result<bool, Error>
pub fn bool(&mut self) -> Result<bool, Error>
Read a bool
from the buffer and progress the internal index. If
a bool
was previously read from the buffer, calling bool()
on the Decoder
again will read a boolean from the same index
without progressing, but instead shifting to read the next bit.
This behavior is symmetric to how the Encoder
stores the bool
s,
and is completely transparent when using the API.
use bitsparrow::Decoder;
// Reading `bools` from a single byte.
let buffer = &[0b11100001];
let mut decoder = Decoder::new(buffer);
assert_eq!(true, decoder.bool().unwrap());
assert_eq!(false, decoder.bool().unwrap());
assert_eq!(false, decoder.bool().unwrap());
assert_eq!(false, decoder.bool().unwrap());
assert_eq!(false, decoder.bool().unwrap());
assert_eq!(true, decoder.bool().unwrap());
assert_eq!(true, decoder.bool().unwrap());
assert_eq!(true, decoder.bool().unwrap());
// Ensure we've read the entire buffer
assert_eq!(true, decoder.end());
Sourcepub fn size(&mut self) -> Result<usize, Error>
pub fn size(&mut self) -> Result<usize, Error>
Read a usize
from the buffer and progress the index. Detailed
explanation on how BitSparrow stores size
can be found on
the homepage.
Sourcepub fn bytes(&mut self) -> Result<&[u8], Error>
pub fn bytes(&mut self) -> Result<&[u8], Error>
Read an arbitary sized binary data from the buffer and progress the index.
Note: BitSparrow internally prefixes bytes
with
size
so you don’t have to worry about how many bytes
you need to read.