Skip to main content

MessageView

Trait MessageView 

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

    // Required methods
    fn decode_view(buf: &'a [u8]) -> Result<Self, DecodeError>;
    fn to_owned_message(&self) -> Self::Owned;

    // Provided methods
    fn decode_view_with_limit(
        buf: &'a [u8],
        _depth: u32,
    ) -> Result<Self, DecodeError> { ... }
    fn to_owned_from_source(&self, source: Option<&Bytes>) -> Self::Owned { ... }
}
Expand description

Trait for zero-copy borrowed message views.

View types borrow from the input buffer and provide read-only access to message fields without allocation. Each generated MyMessageView<'a> implements this trait.

The lifetime 'a ties the view to the input buffer — the view cannot outlive the buffer it was decoded from.

Required Associated Types§

Source

type Owned: Message

The corresponding owned message type.

Required Methods§

Source

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

Decode a view from a buffer, borrowing string/bytes fields directly.

The returned view borrows from buf’s underlying bytes. The caller must ensure the buffer is contiguous (e.g., &[u8] or bytes::Bytes).

Source

fn to_owned_message(&self) -> Self::Owned

Convert this view to the owned message type.

This allocates and copies all borrowed fields. Equivalent to to_owned_from_source(None).

Provided Methods§

Source

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

Decode a view with a custom recursion depth limit.

Used by DecodeOptions::decode_view to pass a non-default recursion budget. The default implementation delegates to decode_view (ignoring the limit); generated code overrides this to call _decode_depth(buf, depth).

Source

fn to_owned_from_source(&self, source: Option<&Bytes>) -> Self::Owned

Convert this view to the owned message type, optionally slicing bytes::Bytes-typed fields from source instead of copying.

When source is the Bytes buffer this view was decoded from, owned fields configured for bytes::Bytes (via the bytes_fields codegen option) are produced via Bytes::slice_ref — a refcount bump, no allocation or copy. Borrowed fields that fall outside source (e.g. on a manually-constructed view) and the None case fall back to Bytes::copy_from_slice.

Generated view types override this; the default delegates to to_owned_message so hand-written impls need only provide that method.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§