Expand description
The variable length integer encoding of u64. This is a simple and fast encoder/decoder.
Features
- integer value length compaction
- minimum support rustc 1.58.1 (db9d1b20b 2022-01-20)
format pattern
Prefix | Precision | Total Bytes |
---|---|---|
0xxxxxxx | 7 bits | 1 byte |
10xxxxxx | 14 bits | 2 bytes |
110xxxxx | 21 bits | 3 bytes |
1110xxxx | 28 bits | 4 bytes |
11110xxx | 35 bits | 5 bytes |
111110xx | 42 bits | 6 bytes |
1111110x | 49 bits | 7 bytes |
11111110 | 56 bits | 8 bytes |
11111111 | 64 bits | 9 bytes |
This format is a like vint64
,
but 0x00 is represented by 0x00.
Examples
Encode
use vu64::encode;
assert_eq!(encode(0x0f0f).as_ref(), &[0x8F, 0x3c]);
Decode
use vu64::decode;
let slice = [0x8F, 0x3c].as_ref();
assert_eq!(decode(slice).unwrap(), 0x0f0f);
Encode and decode
use vu64::{encode, decode};
let val = 1234;
assert_eq!(decode(encode(val).as_ref()).unwrap(), val);
Read from buffer and decode
use vu64::io::ReadVu64;
let vec: Vec<u8> = vec![0xFF, 0xf0, 0xf0, 0x0f, 0x0f, 0xf0, 0xf0, 0x0f, 0x0f];
let mut crsr = std::io::Cursor::new(vec);
let r = crsr.read_and_decode_vu64();
assert!(r.is_ok());
assert_eq!(r.unwrap(), 0x0f0f_f0f0_0f0f_f0f0);
Encode and write to buffer
use vu64::io::WriteVu64;
let vec_0: Vec<u8> = vec![0xFF, 0xf0, 0xf0, 0x0f, 0x0f, 0xf0, 0xf0, 0x0f, 0x0f];
let vec: Vec<u8> = Vec::new();
let mut crsr = std::io::Cursor::new(vec);
let r = crsr.encode_and_write_vu64(0x0f0f_f0f0_0f0f_f0f0);
assert!(r.is_ok());
assert_eq!(crsr.get_ref().as_slice(), vec_0.as_slice());
Modules
Structs
vu64
: serialized variable-length 64-bit integers.Enums
Error type
Constants
Maximum length of a
vu64
in bytesMaximun integer whose length of
vu64
is 1 byte.Maximun integer whose length of
vu64
is 2 byte.Maximun integer whose length of
vu64
is 3 byte.Maximun integer whose length of
vu64
is 4 byte.Maximun integer whose length of
vu64
is 5 byte.Maximun integer whose length of
vu64
is 6 byte.Maximun integer whose length of
vu64
is 7 byte.Maximun integer whose length of
vu64
is 8 byte.Maximun integer whose length of
vu64
is 9 byte.Functions
Decode
vu64
-encoded bytes to unsigned 64-bit integer.Decode
vu64
-encoded bytes to unsigned 64-bit integer.Decode
vu64
-encoded bytes to unsigned 64-bit integer.Get the length of a
vu64
from the first byte.Encode an unsigned 64-bit integer as
vu64
.Get the length of an encoded
vu64
for the given value in bytes.