Expand description
Protobuf scalar type encoding strategies.
Each protobuf scalar type has a specific wire encoding. This module provides encode/decode functions for every scalar type, grouped by wire type.
| Proto type | Rust type | Wire type | Encode fn |
|---|---|---|---|
| double | f64 | Fixed64 | encode_double |
| float | f32 | Fixed32 | encode_float |
| int32 | i32 | Varint | encode_int32 |
| int64 | i64 | Varint | encode_int64 |
| uint32 | u32 | Varint | encode_uint32 |
| uint64 | u64 | Varint | encode_uint64 |
| sint32 | i32 | Varint (zigzag) | encode_sint32 |
| sint64 | i64 | Varint (zigzag) | encode_sint64 |
| fixed32 | u32 | Fixed32 | encode_fixed32 |
| fixed64 | u64 | Fixed64 | encode_fixed64 |
| sfixed32 | i32 | Fixed32 | encode_sfixed32 |
| sfixed64 | i64 | Fixed64 | encode_sfixed64 |
| bool | bool | Varint | encode_bool |
| string | String | LengthDelimited | encode_string |
| bytes | Vec<u8> | LengthDelimited | encode_bytes |
Each encode_<type> also has a fused put_<type>_field sibling that
writes the field tag and the payload in one call (plus
put_len_delimited_header / put_group_start / put_group_end
for message and group framing). Generated write_to bodies use the
fused forms; the encode_* primitives remain the building blocks for
packed payloads and hand-written codecs.
On the decode side, the varint-family decoders have force-inlined
_packed twins (decode_int32_packed etc.) for per-element use
inside packed-repeated loops, where removing the per-element call
boundary is a large measured win. They are not general-purpose
replacements — see decode_int32_packed for the trade-off — and
fixed-width types have no twins because their decoders have no
out-of-line fast path to remove.
§Wire format reference
Fixed-width types are encoded as little-endian bytes on the wire, per the protobuf encoding specification: https://protobuf.dev/programming-guides/encoding/#non-varint_numbers
- Wire type 5 (Fixed32):
float,fixed32,sfixed32— 4 bytes LE - Wire type 1 (Fixed64):
double,fixed64,sfixed64— 8 bytes LE
Varint types (wire type 0) are encoded using the variable-length base-128
encoding. Signed types without zigzag (int32, int64) sign-extend to 64
bits before encoding; negative int32 values therefore always occupy 10
bytes. Use sint32/sint64 (zigzag) when negative values are common.
https://protobuf.dev/programming-guides/encoding/#signed-ints
Structs§
- Field
Payload - A read-only window over exactly one validated length-delimited field.
- Wire
Payload - The raw, length-delimited payload of a
stringorbytesfield, handed toProtoString::from_wire/ProtoBytes::from_wireso a representation can construct itself directly from the wire — validating (or skipping validation) and choosing borrow-vs-own on its own terms.
Constants§
- BOOL_
ENCODED_ LEN - Encoded size of a
boolvalue: always 1 byte. - FIXE
D32_ ENCODED_ LEN - Encoded size of a
float,fixed32, orsfixed32value: always 4 bytes. - FIXE
D64_ ENCODED_ LEN - Encoded size of a
double,fixed64, orsfixed64value: always 8 bytes.
Traits§
- AsShared
Bytes - A value a
bytesfield can encode from: everyProtoBytesrepresentation, plus the borrowed&[u8]that view types carry. - Proto
Bytes - The bound generated code places on the Rust type used for a proto
bytesfield. - Proto
List - The owned collection type backing a proto
repeatedfield. - Proto
String - The bound generated code places on the Rust type used for a proto
stringfield.
Functions§
- borrow_
bytes - Borrow a
bytesvalue directly from the input buffer (zero-copy). - borrow_
group - Borrows the raw bytes of a group body from
buf. - borrow_
str - Borrow a
stringvalue directly from the input buffer (zero-copy). - bytes_
encoded_ len - Compute the encoded byte count of a
bytesvalue (varint length prefix + byte count), excluding the field tag. - decode_
bool - Decode a
boolvalue from a varint. - decode_
bool_ packed - Force-inlined variant of
decode_boolfor packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — seedecode_int32_packedfor when to use the_packedfamily and when to prefer the plain version. - decode_
bytes - Decode a
bytesvalue: read a varint length prefix, then that many bytes. - decode_
bytes_ to - Decode a length-delimited
bytesvalue into a configurableProtoBytestype. - decode_
bytes_ to_ bytes - Decode a
bytesvalue into aBytes: read a varint length prefix, then take that many bytes viaBuf::copy_to_bytes. - decode_
bytes_ to_ ctx decode_bytes_tofor a field of the message thatctxis decoding.- decode_
double - Decode a
doublevalue from 8 bytes little-endian. - decode_
fixed32 - Decode a
fixed32value from 4 bytes little-endian. - decode_
fixed64 - Decode a
fixed64value from 8 bytes little-endian. - decode_
float - Decode a
floatvalue from 4 bytes little-endian. - decode_
int32 - Decode an
int32value from a varint. - decode_
int64 - Decode an
int64value from a varint. - decode_
int32_ packed - Force-inlined variant of
decode_int32for packed-repeated element loops; identical semantics, decodes one element per call. - decode_
int64_ packed - Force-inlined variant of
decode_int64for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — seedecode_int32_packedfor when to use the_packedfamily and when to prefer the plain version. - decode_
sfixed32 - Decode an
sfixed32value from 4 bytes little-endian. - decode_
sfixed64 - Decode an
sfixed64value from 8 bytes little-endian. - decode_
sint32 - Decode a
sint32value from a ZigZag-encoded varint. - decode_
sint64 - Decode a
sint64value from a ZigZag-encoded varint. - decode_
sint32_ packed - Force-inlined variant of
decode_sint32for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — seedecode_int32_packedfor when to use the_packedfamily and when to prefer the plain version. - decode_
sint64_ packed - Force-inlined variant of
decode_sint64for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — seedecode_int32_packedfor when to use the_packedfamily and when to prefer the plain version. - decode_
string - Decode a
stringvalue: read a varint length prefix, then that many bytes, and validate that the bytes are valid UTF-8. - decode_
string_ to - Decode a length-delimited
stringinto a configurableProtoStringtype by dispatching toProtoString::decode_length_delimited. - decode_
string_ to_ ctx decode_string_tofor a field of the message thatctxis decoding.- decode_
uint32 - Decode a
uint32value from a varint. - decode_
uint64 - Decode a
uint64value from a varint. - decode_
uint32_ packed - Force-inlined variant of
decode_uint32for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — seedecode_int32_packedfor when to use the_packedfamily and when to prefer the plain version. - decode_
uint64_ packed - Force-inlined variant of
decode_uint64for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — seedecode_int32_packedfor when to use the_packedfamily and when to prefer the plain version. - encode_
bool - Encode a
boolvalue as a varint (wire type 0). - encode_
bytes - Encode a
bytesvalue as a varint length prefix followed by raw bytes (wire type 2). - encode_
double - Encode a
doublevalue as 8 bytes little-endian (wire type 1). - encode_
fixed32 - Encode a
fixed32value as 4 bytes little-endian (wire type 5). - encode_
fixed64 - Encode a
fixed64value as 8 bytes little-endian (wire type 1). - encode_
float - Encode a
floatvalue as 4 bytes little-endian (wire type 5). - encode_
int32 - Encode an
int32value as a varint (wire type 0). - encode_
int64 - Encode an
int64value as a varint (wire type 0). - encode_
sfixed32 - Encode an
sfixed32value as 4 bytes little-endian (wire type 5). - encode_
sfixed64 - Encode an
sfixed64value as 8 bytes little-endian (wire type 1). - encode_
shared_ bytes - Encode a length-delimited
bytesvalue (varint length prefix + payload, no tag), splicing the payload into segmented sinks by reference count when the value can produce aByteshandle. - encode_
sint32 - Encode a
sint32value as a varint using ZigZag encoding (wire type 0). - encode_
sint64 - Encode a
sint64value as a varint using ZigZag encoding (wire type 0). - encode_
string - Encode a
stringvalue as a varint length prefix followed by UTF-8 bytes (wire type 2). - encode_
uint32 - Encode a
uint32value as a varint (wire type 0). - encode_
uint64 - Encode a
uint64value as a varint (wire type 0). - extend_
packed_ bool - Decode an entire packed
boolpayload intooutin one pass. - extend_
packed_ double - Decode an entire packed
doublepayload intooutin one pass. - extend_
packed_ fixed32 - Decode an entire packed
fixed32payload intooutin one pass. - extend_
packed_ fixed64 - Decode an entire packed
fixed64payload intooutin one pass. - extend_
packed_ float - Decode an entire packed
floatpayload intooutin one pass. - extend_
packed_ int32 - Decode an entire packed
int32payload intooutin one pass. - extend_
packed_ int64 - Decode an entire packed
int64payload intooutin one pass. - extend_
packed_ sfixed32 - Decode an entire packed
sfixed32payload intooutin one pass. - extend_
packed_ sfixed64 - Decode an entire packed
sfixed64payload intooutin one pass. - extend_
packed_ sint32 - Decode an entire packed
sint32payload intooutin one pass. - extend_
packed_ sint64 - Decode an entire packed
sint64payload intooutin one pass. - extend_
packed_ uint32 - Decode an entire packed
uint32payload intooutin one pass. - extend_
packed_ uint64 - Decode an entire packed
uint64payload intooutin one pass. - int32_
encoded_ len - Encoded length of an
int32value in bytes. - int64_
encoded_ len - Encoded length of an
int64value in bytes. - merge_
bytes - Merge length-delimited bytes into an existing
Vec<u8>, reusing its heap allocation when possible. - merge_
string - Merge a length-delimited string into an existing
String, reusing its heap allocation when possible. - put_
bool_ field - Write a tagged
boolfield (tag + one-byte payload). - put_
bytes_ field - Write a tagged
bytesfield (tag + length-prefixed payload). - put_
double_ field - Write a tagged
doublefield (tag + 8-byte payload). - put_
fixed32_ field - Write a tagged
fixed32field (tag + 4-byte payload). - put_
fixed64_ field - Write a tagged
fixed64field (tag + 8-byte payload). - put_
float_ field - Write a tagged
floatfield (tag + 4-byte payload). - put_
group_ end - Write a group field’s
EndGrouptag. - put_
group_ start - Write a group field’s
StartGrouptag. - put_
int32_ field - Write a tagged
int32field (tag + varint payload). - put_
int64_ field - Write a tagged
int64field (tag + varint payload). - put_
len_ delimited_ header - Write a length-delimited field header: tag + payload-length varint.
- put_
sfixed32_ field - Write a tagged
sfixed32field (tag + 4-byte payload). - put_
sfixed64_ field - Write a tagged
sfixed64field (tag + 8-byte payload). - put_
shared_ bytes_ field - Write a tagged
bytesfield, splicing the payload into segmented sinks by reference count when the value can produce aByteshandle. - put_
sint32_ field - Write a tagged
sint32field (tag + zigzag varint payload). - put_
sint64_ field - Write a tagged
sint64field (tag + zigzag varint payload). - put_
string_ field - Write a tagged
stringfield (tag + length-prefixed UTF-8 payload). - put_
uint32_ field - Write a tagged
uint32field (tag + varint payload). - put_
uint64_ field - Write a tagged
uint64field (tag + varint payload). - sint32_
encoded_ len - Encoded length of a
sint32value in bytes. - sint64_
encoded_ len - Encoded length of a
sint64value in bytes. - string_
encoded_ len - Compute the encoded byte count of a
stringvalue (varint length prefix + UTF-8 byte count), excluding the field tag. - uint32_
encoded_ len - Encoded length of a
uint32value in bytes. - uint64_
encoded_ len - Encoded length of a
uint64value in bytes.