Struct Decoder

Source
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>

Source

pub fn new(data: &[u8]) -> Decoder<'_>

Create a new Decoder reading from a &[u8] slice buffer.

Source

pub fn uint8(&mut self) -> Result<u8, Error>

Read a u8 from the buffer and progress the internal index.

Source

pub fn uint16(&mut self) -> Result<u16, Error>

Read a u16 from the buffer and progress the internal index.

Source

pub fn uint32(&mut self) -> Result<u32, Error>

Read a u32 from the buffer and progress the internal index.

Source

pub fn uint64(&mut self) -> Result<u64, Error>

Read a u64 from the buffer and progress the internal index.

Source

pub fn int8(&mut self) -> Result<i8, Error>

Read an i8 from the buffer and progress the internal index.

Source

pub fn int16(&mut self) -> Result<i16, Error>

Read an i16 from the buffer and progress the internal index.

Source

pub fn int32(&mut self) -> Result<i32, Error>

Read an i32 from the buffer and progress the internal index.

Source

pub fn int64(&mut self) -> Result<i64, Error>

Read an i64 from the buffer and progress the internal index.

Source

pub fn float32(&mut self) -> Result<f32, Error>

Read a float32 from the buffer and progress the internal index.

Source

pub fn float64(&mut self) -> Result<f64, Error>

Read a float64 from the buffer and progress the internal index.

Source

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 bools, 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());
Source

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.

Source

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.

Source

pub fn string(&mut self) -> Result<&str, Error>

Read an arbitary sized owned String from the buffer and progress the index.

Note: Analog to bytes, BitSparrow internally prefixes string with size so you don’t have to worry about how many bytes you need to read.

Source

pub fn end(&self) -> bool

Returns true if the entire buffer has been read, otherwise returns false.

Auto Trait Implementations§

§

impl<'a> Freeze for Decoder<'a>

§

impl<'a> RefUnwindSafe for Decoder<'a>

§

impl<'a> Send for Decoder<'a>

§

impl<'a> Sync for Decoder<'a>

§

impl<'a> Unpin for Decoder<'a>

§

impl<'a> UnwindSafe for Decoder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.