Struct Decoder

Source
pub struct Decoder<'b> {
    pub buffer: &'b [u8],
    pub used_bits: i64,
    pub pos: usize,
}

Fields§

§buffer: &'b [u8]§used_bits: i64§pos: usize

Implementations§

Source§

impl<'b> Decoder<'b>

Source

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

Source

pub fn decode<T: Decode<'b>>(&mut self) -> Result<T, Error>

Decode any type that implements Decode.

Source

pub fn integer(&mut self) -> Result<isize, Error>

Decode an integer of any size. This is byte alignment agnostic. First we decode the next 8 bits of the buffer. We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer. If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above, filling in the next 7 least significant bits of the unsigned integer and so on. If the most significant bit was instead 0 we stop decoding any more bits. Finally we use zigzag to convert the unsigned integer back to a signed integer.

Source

pub fn big_integer(&mut self) -> Result<i128, Error>

Decode an integer of 128 bits size. This is byte alignment agnostic. First we decode the next 8 bits of the buffer. We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer. If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above, filling in the next 7 least significant bits of the unsigned integer and so on. If the most significant bit was instead 0 we stop decoding any more bits. Finally we use zigzag to convert the unsigned integer back to a signed integer.

Source

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

Decode a single bit of the buffer to get a bool. We mask out a single bit of the buffer based on used bits. and check if it is 0 for false or 1 for true.

Source

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

Decode a byte from the buffer. This byte alignment agnostic. We use the next 8 bits in the buffer and return the resulting byte.

Source

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

Decode a byte array. Decodes a filler to byte align the buffer, then decodes the next byte to get the array length up to a max of 255. We decode bytes equal to the array length to form the byte array. If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array. We stop once we hit a byte array length of 0. If array length is 0 for first byte array length the we return a empty array.

Source

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

Decode a 32 bit char. This is byte alignment agnostic. First we decode the next 8 bits of the buffer. We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer. If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above, filling in the next 7 least significant bits of the unsigned integer and so on. If the most significant bit was instead 0 we stop decoding any more bits.

Source

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

Source

pub fn utf8(&mut self) -> Result<String, Error>

Decode a string. Convert to byte array and then use byte array decoding. Decodes a filler to byte align the buffer, then decodes the next byte to get the array length up to a max of 255. We decode bytes equal to the array length to form the byte array. If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array. We stop once we hit a byte array length of 0. If array length is 0 for first byte array length the we return a empty array.

Source

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

Decodes a filler of max one byte size. Decodes bits until we hit a bit that is 1. Expects that the 1 is at the end of the current byte in the buffer.

Source

pub fn word(&mut self) -> Result<usize, Error>

Decode a word of any size. This is byte alignment agnostic. First we decode the next 8 bits of the buffer. We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer. If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above, filling in the next 7 least significant bits of the unsigned integer and so on. If the most significant bit was instead 0 we stop decoding any more bits.

Source

pub fn big_word(&mut self) -> Result<u128, Error>

Decode a word of 128 bits size. This is byte alignment agnostic. First we decode the next 8 bits of the buffer. We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer. If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above, filling in the next 7 least significant bits of the unsigned integer and so on. If the most significant bit was instead 0 we stop decoding any more bits.

Source

pub fn decode_list_with<T: Decode<'b>, F>( &mut self, decoder_func: F, ) -> Result<Vec<T>, Error>
where F: Copy + FnOnce(&mut Decoder<'_>) -> Result<T, Error>,

Decode a list of items with a decoder function. This is byte alignment agnostic. Decode a bit from the buffer. If 0 then stop. Otherwise we decode an item in the list with the decoder function passed in. Then decode the next bit in the buffer and repeat above. Returns a list of items decoded with the decoder function.

Source

pub fn bits8(&mut self, num_bits: usize) -> Result<u8, Error>

Decode up to 8 bits. This is byte alignment agnostic. If num_bits is greater than the 8 we throw an IncorrectNumBits error. First we decode the next num_bits of bits in the buffer. If there are less unused bits in the current byte in the buffer than num_bits, then we decode the remaining bits from the most significant bits in the next byte in the buffer. Otherwise we decode the unused bits from the current byte. Returns the decoded value up to a byte in size.

Trait Implementations§

Source§

impl<'b> Debug for Decoder<'b>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'b> Freeze for Decoder<'b>

§

impl<'b> RefUnwindSafe for Decoder<'b>

§

impl<'b> Send for Decoder<'b>

§

impl<'b> Sync for Decoder<'b>

§

impl<'b> Unpin for Decoder<'b>

§

impl<'b> UnwindSafe for Decoder<'b>

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.