Skip to main content

Module types

Module types 

Source
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 typeRust typeWire typeEncode fn
doublef64Fixed64encode_double
floatf32Fixed32encode_float
int32i32Varintencode_int32
int64i64Varintencode_int64
uint32u32Varintencode_uint32
uint64u64Varintencode_uint64
sint32i32Varint (zigzag)encode_sint32
sint64i64Varint (zigzag)encode_sint64
fixed32u32Fixed32encode_fixed32
fixed64u64Fixed64encode_fixed64
sfixed32i32Fixed32encode_sfixed32
sfixed64i64Fixed64encode_sfixed64
boolboolVarintencode_bool
stringStringLengthDelimitedencode_string
bytesVec<u8>LengthDelimitedencode_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§

FieldPayload
A read-only window over exactly one validated length-delimited field.
WirePayload
The raw, length-delimited payload of a string or bytes field, handed to ProtoString::from_wire / ProtoBytes::from_wire so 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 bool value: always 1 byte.
FIXED32_ENCODED_LEN
Encoded size of a float, fixed32, or sfixed32 value: always 4 bytes.
FIXED64_ENCODED_LEN
Encoded size of a double, fixed64, or sfixed64 value: always 8 bytes.

Traits§

AsSharedBytes
A value a bytes field can encode from: every ProtoBytes representation, plus the borrowed &[u8] that view types carry.
ProtoBytes
The bound generated code places on the Rust type used for a proto bytes field.
ProtoList
The owned collection type backing a proto repeated field.
ProtoString
The bound generated code places on the Rust type used for a proto string field.

Functions§

borrow_bytes
Borrow a bytes value directly from the input buffer (zero-copy).
borrow_group
Borrows the raw bytes of a group body from buf.
borrow_str
Borrow a string value directly from the input buffer (zero-copy).
bytes_encoded_len
Compute the encoded byte count of a bytes value (varint length prefix + byte count), excluding the field tag.
decode_bool
Decode a bool value from a varint.
decode_bool_packed
Force-inlined variant of decode_bool for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — see decode_int32_packed for when to use the _packed family and when to prefer the plain version.
decode_bytes
Decode a bytes value: read a varint length prefix, then that many bytes.
decode_bytes_to
Decode a length-delimited bytes value into a configurable ProtoBytes type.
decode_bytes_to_bytes
Decode a bytes value into a Bytes: read a varint length prefix, then take that many bytes via Buf::copy_to_bytes.
decode_bytes_to_ctx
decode_bytes_to for a field of the message that ctx is decoding.
decode_double
Decode a double value from 8 bytes little-endian.
decode_fixed32
Decode a fixed32 value from 4 bytes little-endian.
decode_fixed64
Decode a fixed64 value from 8 bytes little-endian.
decode_float
Decode a float value from 4 bytes little-endian.
decode_int32
Decode an int32 value from a varint.
decode_int64
Decode an int64 value from a varint.
decode_int32_packed
Force-inlined variant of decode_int32 for packed-repeated element loops; identical semantics, decodes one element per call.
decode_int64_packed
Force-inlined variant of decode_int64 for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — see decode_int32_packed for when to use the _packed family and when to prefer the plain version.
decode_sfixed32
Decode an sfixed32 value from 4 bytes little-endian.
decode_sfixed64
Decode an sfixed64 value from 8 bytes little-endian.
decode_sint32
Decode a sint32 value from a ZigZag-encoded varint.
decode_sint64
Decode a sint64 value from a ZigZag-encoded varint.
decode_sint32_packed
Force-inlined variant of decode_sint32 for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — see decode_int32_packed for when to use the _packed family and when to prefer the plain version.
decode_sint64_packed
Force-inlined variant of decode_sint64 for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — see decode_int32_packed for when to use the _packed family and when to prefer the plain version.
decode_string
Decode a string value: 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 string into a configurable ProtoString type by dispatching to ProtoString::decode_length_delimited.
decode_string_to_ctx
decode_string_to for a field of the message that ctx is decoding.
decode_uint32
Decode a uint32 value from a varint.
decode_uint64
Decode a uint64 value from a varint.
decode_uint32_packed
Force-inlined variant of decode_uint32 for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — see decode_int32_packed for when to use the _packed family and when to prefer the plain version.
decode_uint64_packed
Force-inlined variant of decode_uint64 for packed-repeated element loops; identical semantics, decodes one element per call. Not a faster general-purpose decoder — see decode_int32_packed for when to use the _packed family and when to prefer the plain version.
encode_bool
Encode a bool value as a varint (wire type 0).
encode_bytes
Encode a bytes value as a varint length prefix followed by raw bytes (wire type 2).
encode_double
Encode a double value as 8 bytes little-endian (wire type 1).
encode_fixed32
Encode a fixed32 value as 4 bytes little-endian (wire type 5).
encode_fixed64
Encode a fixed64 value as 8 bytes little-endian (wire type 1).
encode_float
Encode a float value as 4 bytes little-endian (wire type 5).
encode_int32
Encode an int32 value as a varint (wire type 0).
encode_int64
Encode an int64 value as a varint (wire type 0).
encode_sfixed32
Encode an sfixed32 value as 4 bytes little-endian (wire type 5).
encode_sfixed64
Encode an sfixed64 value as 8 bytes little-endian (wire type 1).
encode_shared_bytes
Encode a length-delimited bytes value (varint length prefix + payload, no tag), splicing the payload into segmented sinks by reference count when the value can produce a Bytes handle.
encode_sint32
Encode a sint32 value as a varint using ZigZag encoding (wire type 0).
encode_sint64
Encode a sint64 value as a varint using ZigZag encoding (wire type 0).
encode_string
Encode a string value as a varint length prefix followed by UTF-8 bytes (wire type 2).
encode_uint32
Encode a uint32 value as a varint (wire type 0).
encode_uint64
Encode a uint64 value as a varint (wire type 0).
extend_packed_bool
Decode an entire packed bool payload into out in one pass.
extend_packed_double
Decode an entire packed double payload into out in one pass.
extend_packed_fixed32
Decode an entire packed fixed32 payload into out in one pass.
extend_packed_fixed64
Decode an entire packed fixed64 payload into out in one pass.
extend_packed_float
Decode an entire packed float payload into out in one pass.
extend_packed_int32
Decode an entire packed int32 payload into out in one pass.
extend_packed_int64
Decode an entire packed int64 payload into out in one pass.
extend_packed_sfixed32
Decode an entire packed sfixed32 payload into out in one pass.
extend_packed_sfixed64
Decode an entire packed sfixed64 payload into out in one pass.
extend_packed_sint32
Decode an entire packed sint32 payload into out in one pass.
extend_packed_sint64
Decode an entire packed sint64 payload into out in one pass.
extend_packed_uint32
Decode an entire packed uint32 payload into out in one pass.
extend_packed_uint64
Decode an entire packed uint64 payload into out in one pass.
int32_encoded_len
Encoded length of an int32 value in bytes.
int64_encoded_len
Encoded length of an int64 value 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 bool field (tag + one-byte payload).
put_bytes_field
Write a tagged bytes field (tag + length-prefixed payload).
put_double_field
Write a tagged double field (tag + 8-byte payload).
put_fixed32_field
Write a tagged fixed32 field (tag + 4-byte payload).
put_fixed64_field
Write a tagged fixed64 field (tag + 8-byte payload).
put_float_field
Write a tagged float field (tag + 4-byte payload).
put_group_end
Write a group field’s EndGroup tag.
put_group_start
Write a group field’s StartGroup tag.
put_int32_field
Write a tagged int32 field (tag + varint payload).
put_int64_field
Write a tagged int64 field (tag + varint payload).
put_len_delimited_header
Write a length-delimited field header: tag + payload-length varint.
put_sfixed32_field
Write a tagged sfixed32 field (tag + 4-byte payload).
put_sfixed64_field
Write a tagged sfixed64 field (tag + 8-byte payload).
put_shared_bytes_field
Write a tagged bytes field, splicing the payload into segmented sinks by reference count when the value can produce a Bytes handle.
put_sint32_field
Write a tagged sint32 field (tag + zigzag varint payload).
put_sint64_field
Write a tagged sint64 field (tag + zigzag varint payload).
put_string_field
Write a tagged string field (tag + length-prefixed UTF-8 payload).
put_uint32_field
Write a tagged uint32 field (tag + varint payload).
put_uint64_field
Write a tagged uint64 field (tag + varint payload).
sint32_encoded_len
Encoded length of a sint32 value in bytes.
sint64_encoded_len
Encoded length of a sint64 value in bytes.
string_encoded_len
Compute the encoded byte count of a string value (varint length prefix + UTF-8 byte count), excluding the field tag.
uint32_encoded_len
Encoded length of a uint32 value in bytes.
uint64_encoded_len
Encoded length of a uint64 value in bytes.