variable-len-reader 0.2.0

A library for reading variable length data.
Documentation

Variable Len Reader

Crate GitHub last commit GitHub issues GitHub pull requests GitHub

Description

A Rust crate to read variable length data. (VarInt)

Read and write compressed data. Of each such byte, only 7 bits will be used to describe the actual value since its most significant bit indicates whether the next byte is part of the same int. Micro-optimization for int values that are expected to have values below 128.

Usage

Add this to your Cargo.toml:

[dependencies]
variable-len-reader = "*"

Example

use std::net::{TcpListener, TcpStream};
use crate::variable_len::{read_variable_u32, write_variable_u32};

fn main() {
    let addr = "localhost:25564";
    let server = TcpListener::bind(addr).unwrap();
    let mut client = TcpStream::connect(addr).unwrap();
    let mut server = server.incoming().next().unwrap().unwrap();

    // Write
    write_variable_u32(&mut client, 4321).unwrap();

    // Read
    let message = read_variable_u32(&mut server).unwrap();
    assert_eq!(4321, message);
}