pub struct Vlq;Expand description
Formula for Variable-Length Quantity encoding.
If bit 8 is set then bits 0-6 contain length of the value in bytes. Otherwise bits 4-7 contain the length of the value in bytes, and bits 0-3 contain first 4 bits of the encoded value.
If bit 8 is set then bit 7 must be unset. It is reserved for chaining value length to the next byte for big integers from 2^ 63 and larger.
Examples
Type of the value can be different than the type of the serialized value.
let mut buffer = [0u8; 1024];
let size = serialize::<Vlq, u8>(0, &mut buffer).unwrap();
let value = deserialize::<Vlq, u16>(&buffer[..size]).unwrap().0;
assert_eq!(0, value);It may be smaller, unlike fixed size formulas. The only requirement is that the target type is large enough to hold the value.
let mut buffer = [0u8; 1024];
let size = serialize::<Vlq, u32>(8573, &mut buffer).unwrap();
let value = deserialize::<Vlq, u16>(&buffer[..size]).unwrap().0;
assert_eq!(8573, value);If deserialize type can’t fit the value, an error is returned.
let mut buffer = [0u8; 1024];
let size = serialize::<Vlq, u32>(70000, &mut buffer).unwrap();
let err = deserialize::<Vlq, u16>(&buffer[..size]).unwrap_err();
assert!(matches!(err, DeserializeError::IntegerOverflow));Trait Implementations§
source§impl<'de, T> Deserialize<'de, Vlq> for Twhere
T: VlqType,
impl<'de, T> Deserialize<'de, Vlq> for Twhere T: VlqType,
source§fn deserialize(de: Deserializer<'de>) -> Result<Self, DeserializeError>
fn deserialize(de: Deserializer<'de>) -> Result<Self, DeserializeError>
Deserializes value provided deserializer.
Returns deserialized value and the number of bytes consumed from
the and of input. Read more
source§fn deserialize_in_place(
&mut self,
deserializer: Deserializer<'de>
) -> Result<(), DeserializeError>
fn deserialize_in_place( &mut self, deserializer: Deserializer<'de> ) -> Result<(), DeserializeError>
Deserializes value in-place provided deserializer.
Overwrites
self with data from the input. Read more