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
u16varint. - MAX_
LEN_ U32 - Maximum encoded length of a
u32varint. - MAX_
LEN_ U64 - Maximum encoded length of a
u64varint.
Functions§
- decode_
i32 - Decode a zigzag
i32varint (protobufsint32) frombuf. - decode_
i64 - Decode a zigzag
i64varint (protobufsint64) frombuf. - decode_
i32_ canonical - Decode a zigzag
i32varint, rejecting overlong encodings. - decode_
i64_ canonical - Decode a zigzag
i64varint, rejecting overlong encodings. - decode_
u16 - Decode a
u16varint frombuf. - decode_
u32 - Decode a
u32varint frombuf. - decode_
u64 - Decode a
u64varint frombuf. - decode_
u16_ canonical - Decode a
u16varint, rejecting overlong encodings. - decode_
u32_ canonical - Decode a
u32varint, rejecting overlong encodings. - decode_
u64_ canonical - Decode a
u64varint, rejecting overlong encodings. - decode_
usize - Decode a
usizevarint frombuf. - decode_
usize_ canonical - Decode a
usizevarint, rejecting overlong encodings. - encode_
i32 - Encode
valueas a zigzagi32varint (protobufsint32) intobuf. - encode_
i64 - Encode
valueas a zigzagi64varint (protobufsint64) intobuf. - encode_
u16 - Encode
valueas au16varint intobuf. - encode_
u32 - Encode
valueas au32varint intobuf. - encode_
u64 - Encode
valueas au64varint intobuf. - encode_
usize - Encode
valueas ausizevarint intobuf. - encoded_
len_ i32 - Encoded length of
valueas a zigzagi32varint, in bytes (1 to 5). - encoded_
len_ i64 - Encoded length of
valueas a zigzagi64varint, in bytes (1 to 10). - encoded_
len_ u16 - Encoded length of
valueas au16varint, in bytes (1 to 3). - encoded_
len_ u32 - Encoded length of
valueas au32varint, in bytes (1 to 5). - encoded_
len_ u64 - Encoded length of
valueas au64varint, in bytes (1 to 10). - encoded_
len_ usize - Encoded length of
valueas ausizevarint, in bytes (1 to 10).