#minecraft #varint

mc-varint

Minecraft's VarInt and VarLong implemetation in Rust, providing minimum memory usage and maximum performance

2 releases

Uses old Rust 2015

0.1.1 Jul 9, 2018
0.1.0 Jul 8, 2018

#190 in Profiling

Download history 25/week @ 2023-10-21 21/week @ 2023-10-28 23/week @ 2023-11-04 42/week @ 2023-11-11 48/week @ 2023-11-18 29/week @ 2023-11-25 26/week @ 2023-12-02 19/week @ 2023-12-09 28/week @ 2023-12-16 165/week @ 2023-12-23 116/week @ 2023-12-30 24/week @ 2024-01-06 22/week @ 2024-01-13 28/week @ 2024-01-20 20/week @ 2024-01-27 21/week @ 2024-02-03

94 downloads per month
Used in elytra-ping

WTFPL license

11KB
105 lines

mc-varint

Minecraft VarInt and VarLong implemetation in Rust, providing minimum memory usage and maximum performance.

Crates.io WTFPL licensed

Example

Read a VarInt from a Read

extern crate mc_varint;
use mc_varint::{VarInt, VarIntRead};
use std::io::Cursor;
fn main() {
    // firstly we create a Cursor
    let mut cur = Cursor::new(vec![0xff, 0xff, 0xff, 0xff, 0x07]);
    // secondly we read from it
    let var_int = cur.read_var_int().unwrap();
    // the value of var_int is 2147483647
    assert_eq!(var_int, VarInt::from(2147483647));
}

Write a VarInt to a Write

extern crate mc_varint;
use mc_varint::{VarInt, VarIntWrite};
use std::io::Cursor;
fn main() {
    // firstly we create a Cursor and a VarInt
    let mut cur = Cursor::new(Vec::with_capacity(5));
    let var_int = VarInt::from(2147483647);
    // secondly we write the VarInt to the Cursor
    cur.write_var_int(var_int).unwrap();
    // now the var_int is written to cur.
    assert_eq!(cur.into_inner(), vec![0xff, 0xff, 0xff, 0xff, 0x07]);
}

Performance

Platform: 3.4GHz Intel Core i5

running 6 tests
test var_int_convert  ... bench:           7 ns/iter (+/- 0)
test var_int_read     ... bench:          33 ns/iter (+/- 29)
test var_int_write    ... bench:          88 ns/iter (+/- 9)
test var_long_convert ... bench:          10 ns/iter (+/- 1)
test var_long_read    ... bench:          56 ns/iter (+/- 5)
test var_long_write   ... bench:         180 ns/iter (+/- 31)

test result: ok. 0 passed; 0 failed; 0 ignored; 6 measured; 0 filtered out

No runtime deps