Skip to main content

LazyMessageFieldView

Struct LazyMessageFieldView 

Source
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>

Source

pub const fn unset() -> Self

An unset field (the default).

Source

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.

Source

pub const fn is_set(&self) -> bool

Whether the field is present.

Source

pub const fn is_unset(&self) -> bool

Whether the field has no value.

Source

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>

Source

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.

Source

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.

Trait Implementations§

Source§

impl<V> Clone for LazyMessageFieldView<'_, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V> Debug for LazyMessageFieldView<'_, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V> Default for LazyMessageFieldView<'_, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<V> Zeroize for LazyMessageFieldView<'_, V>

Available on crate feature zeroize only.
Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

§

impl<'a, V> Forget for LazyMessageFieldView<'a, V>

§

impl<'a, V> Freeze for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: Freeze,

§

impl<'a, V> Move for LazyMessageFieldView<'a, V>

§

impl<'a, V> RefUnwindSafe for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: RefUnwindSafe,

§

impl<'a, V> Send for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: Send,

§

impl<'a, V> Sync for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: Sync,

§

impl<'a, V> Unpin for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: Unpin,

§

impl<'a, V> UnsafeUnpin for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: UnsafeUnpin,

§

impl<'a, V> UnwindSafe for LazyMessageFieldView<'a, V>
where PhantomData<fn() -> V>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.