pub struct Encoder {
pub buffer: Vec<u8>,
/* private fields */
}
Fields§
§buffer: Vec<u8>
Implementations§
Source§impl Encoder
impl Encoder
pub fn new() -> Encoder
Sourcepub fn encode<T: Encode>(&mut self, x: T) -> Result<&mut Self, Error>
pub fn encode<T: Encode>(&mut self, x: T) -> Result<&mut Self, Error>
Encode any type that implements Encode
.
Sourcepub fn u8(&mut self, x: u8) -> Result<&mut Self, Error>
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
Sourcepub fn bool(&mut self, x: bool) -> &mut Self
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
Sourcepub fn bytes(&mut self, x: &[u8]) -> Result<&mut Self, Error>
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.
Sourcepub fn byte_array(&mut self, arr: &[u8]) -> Result<&mut Self, Error>
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.
Sourcepub fn integer(&mut self, i: isize) -> &mut Self
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.
Sourcepub fn big_integer(&mut self, i: i128) -> &mut Self
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.
Sourcepub fn char(&mut self, c: char) -> &mut Self
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.
pub fn string(&mut self, s: &str) -> &mut Self
Sourcepub fn utf8(&mut self, s: &str) -> Result<&mut Self, Error>
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.
Sourcepub fn word(&mut self, c: usize) -> &mut Self
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.
Sourcepub fn big_word(&mut self, c: u128) -> &mut Self
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.
Sourcepub 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,
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.
Sourcepub fn bits(&mut self, num_bits: i64, val: u8) -> &mut Self
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.