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>
impl<'b> Decoder<'b>
pub fn new(bytes: &'b [u8]) -> Decoder<'_>
Sourcepub fn decode<T: Decode<'b>>(&mut self) -> Result<T, Error>
pub fn decode<T: Decode<'b>>(&mut self) -> Result<T, Error>
Decode any type that implements Decode
.
Sourcepub fn integer(&mut self) -> Result<isize, Error>
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.
Sourcepub fn big_integer(&mut self) -> Result<i128, Error>
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.
Sourcepub fn bool(&mut self) -> Result<bool, Error>
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.
Sourcepub fn u8(&mut self) -> Result<u8, Error>
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.
Sourcepub fn bytes(&mut self) -> Result<Vec<u8>, Error>
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.
Sourcepub fn char(&mut self) -> Result<char, Error>
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.
pub fn string(&mut self) -> Result<String, Error>
Sourcepub fn utf8(&mut self) -> Result<String, Error>
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.
Sourcepub fn filler(&mut self) -> Result<(), Error>
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.
Sourcepub fn word(&mut self) -> Result<usize, Error>
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.
Sourcepub fn big_word(&mut self) -> Result<u128, Error>
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.
Sourcepub fn decode_list_with<T: Decode<'b>, F>(
&mut self,
decoder_func: F,
) -> Result<Vec<T>, Error>
pub fn decode_list_with<T: Decode<'b>, F>( &mut self, decoder_func: F, ) -> Result<Vec<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.
Sourcepub fn bits8(&mut self, num_bits: usize) -> Result<u8, Error>
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.