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

#853 in Data structures

Download history 36/week @ 2024-02-26 3/week @ 2024-03-04 17/week @ 2024-03-11 9/week @ 2024-03-18 18/week @ 2024-04-01 85/week @ 2024-04-15

104 downloads per month
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