Expand description
varing
Protobuf’s varint encoding/decoding for LEB128 friendly types with full const context operations supports.
§Installation
[dependencies]
varing = "0.13"no_std is supported by disabling the default std feature:
[dependencies]
varing = { version = "0.13", default-features = false }§Quick Start
Encode and decode primitive integers as varints:
use varing::Varint;
// Encode
let mut buf = [0u8; u32::MAX_ENCODED_LEN.get()];
let len = 300u32.encode(&mut buf).unwrap();
// Decode
let (bytes_read, decoded) = u32::decode(&buf).unwrap();
assert_eq!(decoded, 300u32);
assert_eq!(bytes_read, len);Signed integers use zigzag encoding, so small absolute values stay compact:
use varing::Varint;
let mut buf = [0u8; i64::MAX_ENCODED_LEN.get()];
let len = (-1i64).encode(&mut buf).unwrap();
assert_eq!(len.get(), 1); // -1 encodes to a single byteEncode/decode sequences:
use varing::{Varint, encode_sequence, decode_sequence, encoded_sequence_len};
let values: Vec<u64> = (0..1024).collect();
let encoded_len = encoded_sequence_len(values.iter());
let mut buf = vec![0; encoded_len];
encode_sequence(values.iter(), &mut buf).unwrap();
let (_bytes_read, decoded) = decode_sequence::<u64, Vec<u64>>(&buf).unwrap();
assert_eq!(decoded, values);All encoding and decoding functions are available in const context as free functions:
use varing::{encode_u32_varint, decode_u32_varint, encoded_u32_varint_len};
const ENCODED: varing::utils::Buffer<6> = encode_u32_varint(300);
const VALUE: u32 = {
let Ok((_len, val)) = decode_u32_varint(ENCODED.as_slice()) else { panic!() };
val
};
assert_eq!(VALUE, 300);
assert_eq!(ENCODED.len(), encoded_u32_varint_len(300).get());§Optional Features
The following feature flags enable varint support for types from third-party crates. Multiple versions of the same crate can be enabled simultaneously.
| Feature | Crate | Notes |
|---|---|---|
arbitrary-int (= v2) | arbitrary-int v1 | Unsigned types + signed types (u1..u127, i1..i127) |
arbitrary-int_1 | arbitrary-int v1 | Unsigned (u1..u127) |
bnum (= v0.13) | bnum | |
chrono (= v0.4) | chrono | Not fully const-compatible |
chrono-tz (= v0.10) | chrono-tz | |
ethereum-types (= v0.16) | ethereum-types | |
ethereum-types_0_16 | ethereum-types v0.16 | |
ethereum-types_0_15 | ethereum-types v0.15 | |
float8 (= v0.4) | float8 | |
half (= v2) | half | |
num-complex (= v0.4) | num-complex | |
num-rational (= v0.4) | num-rational | |
primitive-types (= v0.14) | primitive-types | |
primitive-types_0_14 | primitive-types v0.14 | |
primitive-types_0_13 | primitive-types v0.13 | |
ruint (= v1) | ruint | Not const-compatible |
time (= v0.3) | time |
§Testing
- Property-based testing with
quickcheckfor all types (including optional features). - Fuzz testing with
cargo fuzzfor all types.
§License
varing is under the terms of both the MIT license and the
Apache License (Version 2.0).
See LICENSE-APACHE, LICENSE-MIT for details.
Copyright (c) 2026 Al Liu.
Modules§
- arbitrary_
int arbitrary-int_1orarbitrary-int_2 - LEB128 encoding/decoding for
u1,u2..u127andi1,i2..i127(v2) - bnum
bnum_0_13 - LEB128 encoding/decoding for
bnumtypes. - chrono
chrono_0_4 - LEB128 encoding/decoding for
chronotypes. - chrono_
tz chrono-tz_0_10 - LEB128 encoding/decoding for
chrono-tztypes. - ethereum_
types ethereum-types_0_15orethereum-types_0_16 - LEB128 encoding/decoding for
ethereum-typestypes. - num_
complex num-complex_0_4 - LEB128 encoding/decoding for
num-complextypes. - num_
rational num-rational_0_4 - LEB128 encoding/decoding for
num-rationaltypes. - packable
- Packable trait for types that can be packed into a single value.
- primitive_
types primitive-types_0_13orprimitive-types_0_14 - LEB128 encoding/decoding for
primitive-typestypes. - time
time_0_3 - LEB128 encoding/decoding for
timetypes. - utils
- Utilities for encoding and decoding LEB128 variable length integers.
Structs§
- Insufficient
Data - An error that occurs when trying to decode data from a buffer with insufficient data.
- Insufficient
Space - An error that occurs when trying to write data to a buffer with insufficient space.
- MapDecoder
- An iterator that decodes a sequence of varint values from a buffer.
- Sequence
Decoder - An iterator that decodes a sequence of varint values from a buffer.
Enums§
- Const
Decode Error - Decoding varint error.
- Const
Encode Error - Encode varint error
- Decode
Error - Decoding varint error.
- Encode
Error - Encode varint error
Traits§
- Varint
- A trait for types that can be encoded as variable-length integers (varints).
Functions§
- consume_
varint - Calculates the number of bytes occupied by a varint encoded value in the buffer.
- consume_
varint_ checked - Calculates the number of bytes occupied by a varint encoded value in the buffer.
- decode_
char - Decodes a
charin LEB128 encoded format from the buffer. - decode_
duration - Decodes a
Durationin LEB128 encoded format from the buffer. - decode_
f8e4m3_ varint float8_0_4 - Decodes an
F8E4M3in LEB128 encoded format from the buffer. - decode_
f8e5m2_ varint float8_0_4 - Decodes an
F8E5M2in LEB128 encoded format from the buffer. - decode_
f16_ varint half_2 - Decodes an
f16in LEB128 encoded format from the buffer. - decode_
f32_ varint - Decodes an
f32in LEB128 encoded format from the buffer. - decode_
f64_ varint - Decodes an
f64in LEB128 encoded format from the buffer. - decode_
i8_ varint - Decodes an
i8in LEB128 encoded format from the buffer. - decode_
i16_ varint - Decodes an
i16in LEB128 encoded format from the buffer. - decode_
i32_ varint - Decodes an
i32in LEB128 encoded format from the buffer. - decode_
i64_ varint - Decodes an
i64in LEB128 encoded format from the buffer. - decode_
i128_ varint - Decodes an
i128in LEB128 encoded format from the buffer. - decode_
map - Decodes a collection of entries from the buffer.
- decode_
sequence - Decodes a sequence of values from the buffer.
- decode_
u8_ varint - Decodes a
u8in LEB128 encoded format from the buffer. - decode_
u16_ varint - Decodes a
u16in LEB128 encoded format from the buffer. - decode_
u32_ varint - Decodes a
u32in LEB128 encoded format from the buffer. - decode_
u64_ varint - Decodes a
u64in LEB128 encoded format from the buffer. - decode_
u128_ varint - Decodes a
u128in LEB128 encoded format from the buffer. - encode_
char - Encodes a
charvalue into LEB128 variable length format, and writes it to the buffer. - encode_
char_ to - Encodes a
charvalue into LEB128 variable length format, and writes it to the buffer. - encode_
duration - Encodes a
Durationvalue into LEB128 variable length format, and writes it to the buffer. - encode_
duration_ to - Encodes a
Durationvalue into LEB128 variable length format, and writes it to the buffer. - encode_
f8e4m3_ sequence_ to float8_0_4 - Encodes a sequence of
F8E4M3to the buffer. - encode_
f8e4m3_ varint float8_0_4 - Encodes a
F8E4M3value into LEB128 variable length format, and writes it to the buffer. - encode_
f8e4m3_ varint_ to float8_0_4 - Encodes a
F8E4M3value into LEB128 variable length format, and writes it to the buffer. - encode_
f8e5m2_ sequence_ to float8_0_4 - Encodes a sequence of
F8E5M2to the buffer. - encode_
f8e5m2_ varint float8_0_4 - Encodes a
F8E5M2value into LEB128 variable length format, and writes it to the buffer. - encode_
f8e5m2_ varint_ to float8_0_4 - Encodes a
F8E5M2value into LEB128 variable length format, and writes it to the buffer. - encode_
f16_ sequence_ to half_2 - Encodes a sequence of
f16to the buffer. - encode_
f16_ varint half_2 - Encodes an
f16value into LEB128 variable length format, and writes it to the buffer. - encode_
f16_ varint_ to half_2 - Encodes an
f16value into LEB128 variable length format, and writes it to the buffer. - encode_
f32_ sequence_ to - Encodes a sequence of
f32to the buffer. - encode_
f32_ varint - Encodes an
f32value into LEB128 variable length format, and writes it to the buffer. - encode_
f32_ varint_ to - Encodes an
f32value into LEB128 variable length format, and writes it to the buffer. - encode_
f64_ sequence_ to - Encodes a sequence of
f64to the buffer. - encode_
f64_ varint - Encodes an
f64value into LEB128 variable length format, and writes it to the buffer. - encode_
f64_ varint_ to - Encodes an
f64value into LEB128 variable length format, and writes it to the buffer. - encode_
i8_ sequence_ to - Encodes a sequence of
i8to the buffer. - encode_
i8_ varint - Encodes an
i8value into LEB128 variable length format, and writes it to the buffer. - encode_
i8_ varint_ to - Encodes an
i8value into LEB128 variable length format, and writes it to the buffer. - encode_
i16_ sequence_ to - Encodes a sequence of
i16to the buffer. - encode_
i16_ varint - Encodes an
i16value into LEB128 variable length format, and writes it to the buffer. - encode_
i16_ varint_ to - Encodes an
i16value into LEB128 variable length format, and writes it to the buffer. - encode_
i32_ sequence_ to - Encodes a sequence of
i32to the buffer. - encode_
i32_ varint - Encodes an
i32value into LEB128 variable length format, and writes it to the buffer. - encode_
i32_ varint_ to - Encodes an
i32value into LEB128 variable length format, and writes it to the buffer. - encode_
i64_ sequence_ to - Encodes a sequence of
i64to the buffer. - encode_
i64_ varint - Encodes an
i64value into LEB128 variable length format, and writes it to the buffer. - encode_
i64_ varint_ to - Encodes an
i64value into LEB128 variable length format, and writes it to the buffer. - encode_
i128_ sequence_ to - Encodes a sequence of
i128to the buffer. - encode_
i128_ varint - Encodes an
i128value into LEB128 variable length format, and writes it to the buffer. - encode_
i128_ varint_ to - Encodes an
i128value into LEB128 variable length format, and writes it to the buffer. - encode_
map - Encodes a map of entries as varints and writes them to the buffer.
- encode_
sequence - Encodes a sequence of values as varints and writes them to the buffer.
- encode_
u8_ sequence_ to - Encodes a sequence of
u8to the buffer. - encode_
u8_ varint - Encodes an
u8value into LEB128 variable length format, and writes it to the buffer. - encode_
u8_ varint_ to - Encodes an
u8value into LEB128 variable length format, and writes it to the buffer. - encode_
u16_ sequence_ to - Encodes a sequence of
u16to the buffer. - encode_
u16_ varint - Encodes an
u16value into LEB128 variable length format, and writes it to the buffer. - encode_
u16_ varint_ to - Encodes an
u16value into LEB128 variable length format, and writes it to the buffer. - encode_
u32_ sequence_ to - Encodes a sequence of
u32to the buffer. - encode_
u32_ varint - Encodes an
u32value into LEB128 variable length format, and writes it to the buffer. - encode_
u32_ varint_ to - Encodes an
u32value into LEB128 variable length format, and writes it to the buffer. - encode_
u64_ sequence_ to - Encodes a sequence of
u64to the buffer. - encode_
u64_ varint - Encodes an
u64value into LEB128 variable length format, and writes it to the buffer. - encode_
u64_ varint_ to - Encodes an
u64value into LEB128 variable length format, and writes it to the buffer. - encode_
u128_ sequence_ to - Encodes a sequence of
u128to the buffer. - encode_
u128_ varint - Encodes an
u128value into LEB128 variable length format, and writes it to the buffer. - encode_
u128_ varint_ to - Encodes an
u128value into LEB128 variable length format, and writes it to the buffer. - encoded_
char_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range
char::ENCODED_LEN_RANGE. - encoded_
duration_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range
Duration::ENCODED_LEN_RANGE. - encoded_
f8e4m3_ sequence_ len float8_0_4 - Returns the encoded length of a sequence of
F8E4M3values. - encoded_
f8e4m3_ varint_ len float8_0_4 - Returns the encoded length of the value in LEB128 variable length format. The returned value will be in range of
F8E4M3::ENCODED_LEN_RANGE - encoded_
f8e5m2_ sequence_ len float8_0_4 - Returns the encoded length of a sequence of
F8E5M2values. - encoded_
f8e5m2_ varint_ len float8_0_4 - Returns the encoded length of the value in LEB128 variable length format. The returned value will be in range of
F8E5M2::ENCODED_LEN_RANGE - encoded_
f16_ sequence_ len half_2 - Returns the encoded length of a sequence of
f16values - encoded_
f16_ varint_ len half_2 - Returns the encoded length of the value in LEB128 variable length format. The returned value will be in range of [
f16::ENCODED_LEN_RANGE]. - encoded_
f32_ sequence_ len - Returns the encoded length of a sequence of
f32values - encoded_
f32_ varint_ len - Returns the encoded length of the value in LEB128 variable length format. The returned value will be in range of
f32::ENCODED_LEN_RANGE. - encoded_
f64_ sequence_ len - Returns the encoded length of a sequence of
f64values - encoded_
f64_ varint_ len - Returns the encoded length of the value in LEB128 variable length format. The returned value will be in range of
f64::ENCODED_LEN_RANGE. - encoded_
i8_ sequence_ len - Returns the encoded length of a sequence of
i8values - encoded_
i8_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range of
i8::ENCODED_LEN_RANGE. - encoded_
i16_ sequence_ len - Returns the encoded length of a sequence of
i16values - encoded_
i16_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range of
i16::ENCODED_LEN_RANGE. - encoded_
i32_ sequence_ len - Returns the encoded length of a sequence of
i32values - encoded_
i32_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range of
i32::ENCODED_LEN_RANGE. - encoded_
i64_ sequence_ len - Returns the encoded length of a sequence of
i64values - encoded_
i64_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range
i64::ENCODED_LEN_RANGE. - encoded_
i128_ sequence_ len - Returns the encoded length of a sequence of
i128values - encoded_
i128_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range
i128::ENCODED_LEN_RANGE. - encoded_
map_ len - Returns the total length of a map of entries.
- encoded_
sequence_ len - Returns the total number of bytes needed to encode a sequence of values.
- encoded_
u8_ sequence_ len - Returns the encoded length of a sequence of
u8values - encoded_
u8_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range of
u8::ENCODED_LEN_RANGE. - encoded_
u16_ sequence_ len - Returns the encoded length of a sequence of
u16values - encoded_
u16_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range of
u16::ENCODED_LEN_RANGE. - encoded_
u32_ sequence_ len - Returns the encoded length of a sequence of
u32values - encoded_
u32_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range of
u32::ENCODED_LEN_RANGE. - encoded_
u64_ sequence_ len - Returns the encoded length of a sequence of
u64values - encoded_
u64_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range
u64::ENCODED_LEN_RANGE. - encoded_
u128_ sequence_ len - Returns the encoded length of a sequence of
u128values - encoded_
u128_ varint_ len - Returns the encoded length of the value in LEB128 variable length format.
The returned value will be in range
u128::ENCODED_LEN_RANGE. - map_
decoder - Returns a sequence decoder for the given buffer.
- sequence_
decoder - Returns a sequence decoder for the given buffer.
- try_
consume_ varint - Calculates the number of bytes occupied by a varint encoded value in the buffer.
Type Aliases§
- Char
Buffer - A buffer for storing LEB128 encoded
charvalue.