Crate mc_varint

Source
Expand description

An implementation for Minecraft1’s VarInt and VarLong types, focusing on minimum memory usage and maximum performance.

This crate contains two structs for VarInt and VarLong, and four traits for conversations and IO operations on them. You may refer to the paragraphs following to get their usages.

Algorithms and structures herein are built according to a wiki.vg page

§VarInt and VarLong struct

These two structs represents the two types mentioned above. Data stored in these two structs are guaranteed a valid VarInt or VarLong by their conversation traits.

You may create these structs using function VarInt::from(i32) and VarLong::from(i64). And using i32::from(VarInt) and i64::from(VarLong) can simply convert two structs into actual values in order to use them in following logic.

These two structs implements Default, which leads to easier use in codes.

§Two ‘Read’ traits and two ‘Write’ traits

They are VarIntRead, VarLongRead for ‘Read’, and VarIntWrite, VarLongWrite for ‘Write’.

Both two ‘Read’ traits are implemented for all R’s where R: io::Read. You may use it to read VarInt’s and VarLong’s directly from IO streams, such as, network connections or files.

And for the two ‘Write’ traits, they are implemented for all W’s where W: io::Write for your convenience.

§How this crate reduces memory usage

As only VarInt and VarLong struct performs the allocation, firstly we should minimize the space these two structs use in memory. As These two structs only stores the sized integer data instead of something combined with pointers and sizes, the memory usage is reduced to minimal, which means, the VarInt only uses 5 bytes and VarLong only uses 10.

When writing to IO, reading from IO or performing type conversations, this crate only allocate one [u8; 1] array as buffer, and for the Rust’s sake, can free it safely even without a GC. By this way we save more memory in calculating, resulting in more memory able to be used for network buffers, databases and your following logic code.


  1. A well-known video game whose servers and clients are able to be built by third-party authors. 

Structs§

VarInt
The struct representing a VarInt or VarLong.
VarLong
The struct representing a VarInt or VarLong.

Traits§

VarIntRead
The Read trait for this VarInt or VarLong struct.
VarIntWrite
The Write trait for this VarInt or VarLong struct.
VarLongRead
The Read trait for this VarInt or VarLong struct.
VarLongWrite
The Write trait for this VarInt or VarLong struct.