Skip to main content

Module varint

Module varint 

Source
Expand description

Unsigned LEB128 varint encoding, plus zigzag-mapped signed helpers.

A varint encodes an unsigned integer in one to ten bytes. The low seven bits of each byte carry payload, and the top bit signals continuation: 1 means more bytes follow, 0 means the value is complete.

The encoding is identical to the format used by Protocol Buffers, WebAssembly, and DWARF. The signed helpers (encode_i32, decode_i64, …) apply the zigzag mapping first, matching protobuf’s sint32 / sint64 wire types.

§Error contract

Every function in this module is atomic: on error the cursor is left exactly where it was. A streaming caller that receives Error::UnexpectedEof can append more bytes and retry the same call without cloning the cursor first.

§Canonical encodings

LEB128 allows a value to be padded with redundant 0x80 continuation bytes followed by a 0x00 terminator (for example [0x80, 0x00] is an overlong encoding of 0). The plain decode_* functions accept these overlong forms, as Protocol Buffers does. The decode_*_canonical functions reject them with Error::InvalidEncoding, so that every value has exactly one accepted wire representation. Use the canonical forms when the encoded bytes are hashed, signed, deduplicated, or compared between implementations. The encoders always produce the canonical form.

Constants§

MAX_LEN_U16
Maximum encoded length of a u16 varint.
MAX_LEN_U32
Maximum encoded length of a u32 varint.
MAX_LEN_U64
Maximum encoded length of a u64 varint.

Functions§

decode_i32
Decode a zigzag i32 varint (protobuf sint32) from buf.
decode_i64
Decode a zigzag i64 varint (protobuf sint64) from buf.
decode_i32_canonical
Decode a zigzag i32 varint, rejecting overlong encodings.
decode_i64_canonical
Decode a zigzag i64 varint, rejecting overlong encodings.
decode_u16
Decode a u16 varint from buf.
decode_u32
Decode a u32 varint from buf.
decode_u64
Decode a u64 varint from buf.
decode_u16_canonical
Decode a u16 varint, rejecting overlong encodings.
decode_u32_canonical
Decode a u32 varint, rejecting overlong encodings.
decode_u64_canonical
Decode a u64 varint, rejecting overlong encodings.
decode_usize
Decode a usize varint from buf.
decode_usize_canonical
Decode a usize varint, rejecting overlong encodings.
encode_i32
Encode value as a zigzag i32 varint (protobuf sint32) into buf.
encode_i64
Encode value as a zigzag i64 varint (protobuf sint64) into buf.
encode_u16
Encode value as a u16 varint into buf.
encode_u32
Encode value as a u32 varint into buf.
encode_u64
Encode value as a u64 varint into buf.
encode_usize
Encode value as a usize varint into buf.
encoded_len_i32
Encoded length of value as a zigzag i32 varint, in bytes (1 to 5).
encoded_len_i64
Encoded length of value as a zigzag i64 varint, in bytes (1 to 10).
encoded_len_u16
Encoded length of value as a u16 varint, in bytes (1 to 3).
encoded_len_u32
Encoded length of value as a u32 varint, in bytes (1 to 5).
encoded_len_u64
Encoded length of value as a u64 varint, in bytes (1 to 10).
encoded_len_usize
Encoded length of value as a usize varint, in bytes (1 to 10).