Crate vartyint

source ·
Expand description

Read & Write varint to/from bytes

Writing bytes

use vartyint;
let mut my_bytes = Vec::new();
vartyint::write_i32(1000, &mut my_bytes);
assert_eq!(my_bytes, &[0xd0, 0x0f]);

Reading

Read an integer from a slice of bytes (&[u8]). Upon success, the number, as well as the rest of the bytes is returned. You can “pop off” numbers like this.

use vartyint;
let my_bytes = vec![0x18, 0x01, 0xBF, 0xBB, 0x01];

let (num1, my_bytes) = vartyint::read_i32(&my_bytes).unwrap();
assert_eq!(num1, 12);
assert_eq!(my_bytes, &[0x01, 0xBF, 0xBB, 0x01]);

let (num2, my_bytes) = vartyint::read_i32(&my_bytes).unwrap();
assert_eq!(num2, -1);
assert_eq!(my_bytes, &[0xBF, 0xBB, 0x01]);

let (num3, my_bytes) = vartyint::read_i32(&my_bytes).unwrap();
assert_eq!(num3, -12_000);
assert_eq!(my_bytes, &[]);

// Can't read any more
assert_eq!(vartyint::read_i32(&my_bytes), Err(vartyint::VartyIntError::EmptyBuffer));

Enums

Traits

Functions