Struct Encoder

Source
pub struct Encoder {
    pub buffer: Vec<u8>,
    /* private fields */
}

Fields§

§buffer: Vec<u8>

Implementations§

Source§

impl Encoder

Source

pub fn new() -> Encoder

Source

pub fn encode<T: Encode>(&mut self, x: T) -> Result<&mut Self, Error>

Encode any type that implements Encode.

Source

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

Encode 1 unsigned byte. Uses the next 8 bits in the buffer, can be byte aligned or byte unaligned

Source

pub fn bool(&mut self, x: bool) -> &mut Self

Encode a bool value. This is byte alignment agnostic. Uses the next unused bit in the current byte to encode this information. One for true and Zero for false

Source

pub fn bytes(&mut self, x: &[u8]) -> Result<&mut Self, Error>

Encode a byte array. Uses filler to byte align the buffer, then writes byte array length up to 255. Following that it writes the next 255 bytes from the array. We repeat writing length up to 255 and the next 255 bytes until we reach the end of the byte array. After reaching the end of the byte array we write a 0 byte. Only write 0 byte if the byte array is empty.

Source

pub fn byte_array(&mut self, arr: &[u8]) -> Result<&mut Self, Error>

Encode a byte array in a byte aligned buffer. Throws exception if any bits for the current byte were used. Writes byte array length up to 255 Following that it writes the next 255 bytes from the array. We repeat writing length up to 255 and the next 255 bytes until we reach the end of the byte array. After reaching the end of the buffer we write a 0 byte. Only write 0 if the byte array is empty.

Source

pub fn integer(&mut self, i: isize) -> &mut Self

Encode an integer of any size. This is byte alignment agnostic. First we use zigzag once to double the number and encode the negative sign as the least significant bit. Next we encode the 7 least significant bits of the unsigned integer. If the number is greater than 127 we encode a leading 1 followed by repeating the encoding above for the next 7 bits and so on.

Source

pub fn big_integer(&mut self, i: i128) -> &mut Self

Encode an integer of 128 bits size. This is byte alignment agnostic. First we use zigzag once to double the number and encode the negative sign as the least significant bit. Next we encode the 7 least significant bits of the unsigned integer. If the number is greater than 127 we encode a leading 1 followed by repeating the encoding above for the next 7 bits and so on.

Source

pub fn char(&mut self, c: char) -> &mut Self

Encode a char of 32 bits. This is byte alignment agnostic. We encode the 7 least significant bits of the unsigned byte. If the char value is greater than 127 we encode a leading 1 followed by repeating the above for the next 7 bits and so on.

Source

pub fn string(&mut self, s: &str) -> &mut Self

Source

pub fn utf8(&mut self, s: &str) -> Result<&mut Self, Error>

Encode a string. Convert to byte array and then use byte array encoding. Uses filler to byte align the buffer, then writes byte array length up to 255. Following that it writes the next 255 bytes from the array. After reaching the end of the buffer we write a 0 byte. Only write 0 byte if the byte array is empty.

Source

pub fn word(&mut self, c: usize) -> &mut Self

Encode a unsigned integer of any size. This is byte alignment agnostic. We encode the 7 least significant bits of the unsigned byte. If the char value is greater than 127 we encode a leading 1 followed by repeating the above for the next 7 bits and so on.

Source

pub fn big_word(&mut self, c: u128) -> &mut Self

Encode a unsigned integer of 128 bits size. This is byte alignment agnostic. We encode the 7 least significant bits of the unsigned byte. If the char value is greater than 127 we encode a leading 1 followed by repeating the above for the next 7 bits and so on.

Source

pub fn encode_list_with<T>( &mut self, list: &[T], encoder_func: for<'r> fn(&T, &'r mut Encoder) -> Result<(), Error>, ) -> Result<&mut Self, Error>
where T: Encode,

Encode a list of bytes with a function This is byte alignment agnostic. If there are bytes in a list then write 1 bit followed by the functions encoding. After the last item write a 0 bit. If the list is empty only encode a 0 bit.

Source

pub fn bits(&mut self, num_bits: i64, val: u8) -> &mut Self

Encodes up to 8 bits of information and is byte alignment agnostic. Uses unused bits in the current byte to write out the passed in byte value. Overflows to the most significant digits of the next byte if number of bits to use is greater than unused bits. Expects that number of bits to use is greater than or equal to required bits by the value. The param num_bits is i64 to match unused_bits type.

Trait Implementations§

Source§

impl Default for Encoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.