Struct alkahest::Vlq

source ·
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,

source§

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>

Deserializes value in-place provided deserializer. Overwrites self with data from the input. Read more
source§

impl Formula for Vlq

source§

const MAX_STACK_SIZE: Option<usize> = None

Maximum size of stack this formula occupies.
source§

const EXACT_SIZE: bool = true

Signals that MAX_STACK_SIZE is accurate.
source§

const HEAPLESS: bool = true

Signals that heap is not used for serialzation.
source§

impl<T> Serialize<Vlq> for Twhere T: VlqType,

source§

fn size_hint(&self) -> Option<Sizes>

Returns heap and stack sizes required to serialize self. If some sizes are returned they must be exact. Read more
source§

fn serialize<B>(self, sizes: &mut Sizes, buffer: B) -> Result<(), B::Error>where B: Buffer,

Serializes self into the given buffer. heap specifies the size of the buffer’s heap occupied prior to this call. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Vlq

§

impl Send for Vlq

§

impl Sync for Vlq

§

impl Unpin for Vlq

§

impl UnwindSafe for Vlq

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.