embedded_jsonrpc

Module varint

Source
Expand description

§Varint Encoding and Decoding Library

This crate provides utilities for encoding and decoding unsigned integers (usize) using the variable-length integer (varint) format. Varints are commonly used in protocols and file formats like Protocol Buffers to efficiently encode integers of varying sizes.

§Functionality

  • encode - Encodes a usize value into a buffer as a varint and returns the number of bytes written.
  • decode - Decodes a varint from a byte buffer, returning the value and the number of bytes read.

§Examples

§Encoding a Value

use embedded_jsonrpc::varint::encode;

let mut buffer = [0u8; 10]; // Create a buffer with sufficient size
let bytes_written = encode(300, &mut buffer);
println!("Encoded 300 into {:?} using {} bytes", &buffer[..bytes_written], bytes_written);

§Decoding a Value

use embedded_jsonrpc::varint::decode;

let buffer = [0xac, 0x02]; // Encoded varint for 300
if let Some((value, bytes_read)) = decode(&buffer) {
    println!("Decoded value: {}, bytes read: {}", value, bytes_read);
} else {
    println!("Failed to decode value");
}

§License

This crate is licensed under the Mozilla Public License 2.0 (MPL-2.0). See the LICENSE file for more details.

Functions§

  • Decodes a varint from the provided buffer. Returns the decoded value and the number of bytes read, or None if the buffer is incomplete.
  • Encodes a usize into a varint and writes it into the provided buffer. Returns the number of bytes written.