Skip to main content

decode_unknown_field

Function decode_unknown_field 

Source
pub fn decode_unknown_field(
    tag: Tag,
    buf: &mut impl Buf,
    ctx: DecodeContext<'_>,
) -> Result<UnknownField, DecodeError>
Expand description

Decode one unknown field’s value from buf and return it as an UnknownField.

The tag must already have been decoded; this function reads only the payload that follows it on the wire. Groups are decoded recursively until their matching EndGroup tag.

ctx carries the remaining nesting depth and the shared unknown-field allowance. For group fields this function calls itself recursively, consuming one depth level each time. When the depth reaches zero DecodeError::RecursionLimitExceeded is returned. Construct a fresh DecodeContext at the outermost call site; generated code passes the ctx value received by the enclosing merge.

§Errors

Returns an error if the buffer is truncated, the wire type is EndGroup (which indicates a structural mismatch in the wire data), the recursion limit is exceeded, or the unknown-field limit is exceeded.

§Allocation

Unknown fields can occupy far more memory decoded than encoded: a 2-byte varint field becomes a ~40-byte UnknownField, so an input-size cap alone does not bound decoder memory. Every decoded field consumes one slot of the context’s shared unknown-field allowance before it is materialized; when the allowance is exhausted decoding fails with DecodeError::UnknownFieldLimitExceeded. Length-delimited payload bytes are bounded by the input itself (the buf.remaining() < len check forces the sender to actually deliver them), so they are not counted against the limit — cap the input size to bound them.