VLQDecode

Trait VLQDecode 

Source
pub trait VLQDecode<T> {
    // Required method
    fn read_vlq(&mut self) -> Result<T>;
}

Required Methods§

Source

fn read_vlq(&mut self) -> Result<T>

Read a VLQ byte array from stream and decode it to an integer.

§Examples
use vlqencoding::VLQDecode;
use std::io::{Cursor,Seek,SeekFrom,ErrorKind};

let mut c = Cursor::new(vec![120u8, 211, 171, 202, 220, 84]);

let x: Result<u8, _> = c.read_vlq();
assert_eq!(x.unwrap(), 120u8);

let x: Result<u16, _> = c.read_vlq();
assert_eq!(x.unwrap_err().kind(), ErrorKind::InvalidData);

c.seek(SeekFrom::Start(1)).expect("seek should work");
let x: Result<u64, _> = c.read_vlq();
assert_eq!(x.unwrap(), 22742734291u64);

Signed integers are decoded via zig-zag:

use vlqencoding::VLQDecode;
use std::io::{Cursor,Seek,SeekFrom,ErrorKind};

let mut c = Cursor::new(vec![5u8, 208, 15]);

let x: Result<i8, _> = c.read_vlq();
assert_eq!(x.unwrap(), -3i8);

let x: Result<i8, _> = c.read_vlq();
assert_eq!(x.unwrap_err().kind(), ErrorKind::InvalidData);

c.seek(SeekFrom::Start(1)).expect("seek should work");
let x: Result<i32, _> = c.read_vlq();
assert_eq!(x.unwrap(), 1000i32);

Implementors§

Source§

impl<R: Read + ?Sized> VLQDecode<i8> for R

Source§

impl<R: Read + ?Sized> VLQDecode<i16> for R

Source§

impl<R: Read + ?Sized> VLQDecode<i32> for R

Source§

impl<R: Read + ?Sized> VLQDecode<i64> for R

Source§

impl<R: Read + ?Sized> VLQDecode<isize> for R

Source§

impl<R: Read + ?Sized> VLQDecode<u8> for R

Source§

impl<R: Read + ?Sized> VLQDecode<u16> for R

Source§

impl<R: Read + ?Sized> VLQDecode<u32> for R

Source§

impl<R: Read + ?Sized> VLQDecode<u64> for R

Source§

impl<R: Read + ?Sized> VLQDecode<usize> for R