Skip to main content

LazyMessageView

Trait LazyMessageView 

Source
pub trait LazyMessageView<'a>: Sized {
    type Owned: Message;

    // Required methods
    fn decode_lazy(buf: &'a [u8]) -> Result<Self, DecodeError>;
    fn merge_lazy(
        &mut self,
        buf: &'a [u8],
        ctx: DecodeContext<'_>,
    ) -> Result<(), DecodeError>;
    fn to_owned_message(&self) -> Result<Self::Owned, DecodeError>;

    // Provided method
    fn decode_lazy_with_ctx(
        buf: &'a [u8],
        ctx: DecodeContext<'_>,
    ) -> Result<Self, DecodeError> { ... }
}
Expand description

The trait implemented by generated lazy view types (FooLazyView<'a>).

Lazy views are a separate, additive type family generated alongside the eager FooView family under the lazy_views codegen option. Where MessageView’s contract is “decode succeeded ⇒ the whole tree was validated”, a lazy view’s decode_lazy performs a single non-recursive scan over the message’s own fields: scalar, string, and bytes fields are borrowed exactly as in the eager view, while nested and repeated message fields are recorded as undecoded byte ranges (see LazyMessageFieldView / LazyRepeatedView) and decoded only on access. Deferred validation is therefore visible in the type and trait bound — generic code over MessageView never silently inherits it.

Generated lazy-view structs may gain fields across releases; see the struct evolution policy on Message.

Required Associated Types§

Source

type Owned: Message

The corresponding owned message type.

Required Methods§

Source

fn decode_lazy(buf: &'a [u8]) -> Result<Self, DecodeError>

Decode a lazy view from buf: one scan over the message’s own fields, deferring nested message fields.

§Errors

Returns DecodeError if the message’s own fields are malformed, or DecodeError::ElementMemoryLimitExceeded if recording its repeated elements exceeds the default element-memory budget — the Vec of deferred byte ranges is real memory even though the elements themselves are not decoded yet. Deferred sub-message bytes are not validated here; they surface errors on access.

Source

fn merge_lazy( &mut self, buf: &'a [u8], ctx: DecodeContext<'_>, ) -> Result<(), DecodeError>

Merge fields decoded from buf into this view (proto merge semantics: singular scalars last-wins, repeated append, deferred message fragments accumulate).

Used by LazyMessageFieldView::get to reassemble a field whose value was split across multiple wire occurrences — application code rarely calls this directly.

§Errors

Same contract as decode_lazy_with_ctx.

Source

fn to_owned_message(&self) -> Result<Self::Owned, DecodeError>

Convert this view to the owned message type.

This decodes every deferred sub-message, so it is where deferred validation errors surface. Each deferred subtree decodes under its own replayed unknown-field allowance (see LazyMessageFieldView::get), so the conversion’s total unknown-field records are bounded per subtree, not globally as in an eager decode.

§Errors

Returns the DecodeError that accessing a malformed or over-budget deferred field would have reported. Unlike MessageView::to_owned_message, this conversion can genuinely fail for a wire-decoded lazy view: deferred subtrees were never validated (or charged against the unknown-field allowance) at decode time, so their errors surface here.

Provided Methods§

Source

fn decode_lazy_with_ctx( buf: &'a [u8], ctx: DecodeContext<'_>, ) -> Result<Self, DecodeError>

Decode a lazy view under custom decode limits.

Used by DecodeOptions::decode_lazy_view. The budgets remaining at each deferred field’s position are recorded and charged when that field is accessed, so custom limits flow through deferred decoding. The default implementation delegates to decode_lazy and ignores the context; generated code always overrides it.

§Errors

Same contract as decode_lazy, plus DecodeError::RecursionLimitExceeded, DecodeError::UnknownFieldLimitExceeded, or DecodeError::ElementMemoryLimitExceeded when ctx’s budgets are exhausted by the message’s own fields.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§