#varint

vartyint

Read & Write varint encoded integers

6 releases

0.3.0 Oct 17, 2023
0.2.0 Sep 22, 2023
0.2.0-rc2 Sep 8, 2023
0.2.0-rc1 Sep 5, 2023
0.1.0 Jun 14, 2020

#17 in #varint


Used in osm-lump-ways

MIT/Apache

20KB
514 lines

vartyint

A rust library for reading varints

Check the Documentation


lib.rs:

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));

No runtime deps