pub struct LazyMessageFieldView<'a, V> { /* private fields */ }Expand description
A deferred view of a singular message field on a lazy view.
Unlike MessageFieldView — which eagerly decodes (and boxes) the
sub-message during decode — this stores only the field’s undecoded wire
bytes and decodes a fresh V on each get, so decoding the
enclosing message does not allocate or recurse into sub-messages the
caller never reads.
get returns a freshly-decoded view each call (views are thin borrows,
so this is cheap) and does not cache — bind the result when reading
several fields.
§Merge semantics
A singular message field may legally appear more than once on the wire;
decoders must merge the occurrences. This type stores each occurrence’s
bytes as a separate fragment and get replays them in order
(decode the first, LazyMessageView::merge_lazy the rest), so the
result matches the eager and owned decoders.
§Deferred validation and budgets
The fragment bytes are not validated when the enclosing view is
decoded; a malformed sub-message surfaces as a DecodeError from
get. The recursion budget, unknown-field allowance, and
element-memory budget remaining when the field was recorded are stored
alongside the fragments,
and each access replays them as a fresh per-subtree budget (see
get for the approximation this implies). Deep lazy chains
fail with DecodeError::RecursionLimitExceeded at the same boundary as
the eager decoder, and custom limits passed to the enclosing
decode_lazy_with_ctx flow
through.
§Re-encoding
ViewEncode on the enclosing lazy view replays the recorded fragments
byte-for-byte without validating them — re-encoding a never-accessed
malformed field round-trips its bytes silently.
Implementations§
Source§impl<'a, V> LazyMessageFieldView<'a, V>
impl<'a, V> LazyMessageFieldView<'a, V>
Sourcepub const fn from_bytes(raw: &'a [u8]) -> Self
pub const fn from_bytes(raw: &'a [u8]) -> Self
A set field carrying the sub-message’s undecoded wire bytes, with the default recursion, unknown-field, and element-memory budgets for access.
Sourcepub fn fragments(&self) -> &[&'a [u8]]
pub fn fragments(&self) -> &[&'a [u8]]
The undecoded wire fragments, in wire order (empty if unset).
A singular message field that appeared exactly once on the wire — the
common case — yields one fragment. Encoders that split the field
across multiple occurrences yield one fragment per occurrence;
get merges them per proto semantics.
Source§impl<'a, V: LazyMessageView<'a>> LazyMessageFieldView<'a, V>
impl<'a, V: LazyMessageView<'a>> LazyMessageFieldView<'a, V>
Sourcepub fn get(&self) -> Result<Option<V>, DecodeError>
pub fn get(&self) -> Result<Option<V>, DecodeError>
Decode and return the sub-message view, or None if unset.
Multiple wire fragments are merged per proto semantics (see the type
docs). The view is re-decoded on every call; there is no cache — bind
the result when reading several fields. Note the shape difference
from LazyRepeatedView::get, which returns Option<Result<V, _>>.
Each access rebuilds a fresh decode context from the budgets recorded
at decode time, so every deferred subtree independently gets the full
recorded unknown-field allowance and element-memory budget rather than
sharing one pool with its siblings (the original decode call’s shared
budgets are gone by access time). Both are therefore per-subtree
bounds on the lazy path, not the global decode-time caps the eager
decoder enforces: a full traversal can materialize records
proportional to input size, where eager
decode_view rejects such input
up front. Prefer the eager path for untrusted input if that global
bound matters.
§Errors
Returns DecodeError if the deferred bytes are not a valid
encoding of V — validation happens here, not when the enclosing
view was decoded — DecodeError::RecursionLimitExceeded when the
recursion budget recorded at decode time is exhausted,
DecodeError::UnknownFieldLimitExceeded when the unknown-field
allowance is, or DecodeError::ElementMemoryLimitExceeded when the
element-memory budget is.
Sourcepub fn get_or_default(&self) -> Result<V, DecodeError>where
V: Default,
pub fn get_or_default(&self) -> Result<V, DecodeError>where
V: Default,
Like get, but an unset field decodes to the default
view — the lazy analogue of MessageFieldView’s deref-to-default,
for the common read path:
let city = view.address.get_or_default()?.city;§Errors
Same as get.