Skip to main content

HasMessageView

Trait HasMessageView 

Source
pub trait HasMessageView: Sized + Message {
    type View<'a>: MessageView<'a, Owned = Self> + Send + Sync;
    type ViewHandle: From<OwnedView<Self::View<'static>>> + AsRef<OwnedView<Self::View<'static>>> + Send + Sync + 'static;

    // Provided methods
    fn decode_view(buf: &[u8]) -> Result<Self::View<'_>, DecodeError> { ... }
    fn decode_view_with_options<'a>(
        buf: &'a [u8],
        opts: &DecodeOptions,
    ) -> Result<Self::View<'a>, DecodeError> { ... }
    fn decode_view_handle(bytes: Bytes) -> Result<Self::ViewHandle, DecodeError> { ... }
    fn decode_view_handle_with_options(
        bytes: Bytes,
        opts: &DecodeOptions,
    ) -> Result<Self::ViewHandle, DecodeError> { ... }
}
Expand description

Re-export of buffa’s view-family trait.

Generated buffa code implements this for every message (when views are generated), linking the owned type to its borrowed view (Req::View<'a>) and its 'static handle (Req::ViewHandle, the FooOwnedView wrapper). ServiceRequest<'_, Req> and StreamMessage<Req> are bounded on it.

The per-field accessor methods (msg.name(), …) are inherent on the concrete generated wrapper and on the view’s public fields; code that is generic over M: HasMessageView reaches the message through as_ref()/view / to_owned_message instead of named fields. Links an owned message type to its generated zero-copy view types.

For a message Foo, generated code implements this trait as View<'a> = FooView<'a> (the borrowed view) and ViewHandle = FooOwnedView (the self-contained 'static handle). The trait lets code that is generic over an owned message name those types — for example an RPC framework that decodes M::View<'_> from a request body it owns, or holds M::ViewHandle items in a stream — without per-message glue on the consumer’s side.

The associated types intentionally carry only structural bounds:

  • View<'a> is the message’s view, with Owned = Self.
  • ViewHandle is convertible from, and exposes via AsRef, the corresponding OwnedView<Self::View<'static>>, so generic code can reach reborrow, bytes, and to_owned_message without naming the concrete wrapper. The wrapper’s per-field accessor methods remain inherent on the concrete type.

Generic code that wants to reborrow through the handle (handle.as_ref().reborrow()) adds M::View<'static>: ViewReborrow as a bound at the use site; every generated view satisfies it. (The bound cannot live on the trait itself: a where Self::View<'static>: ViewReborrow clause currently trips a GAT normalization error, E0308 “expected MessageView<'a>, found MessageView<'static>”.)

§Implementing

Implementations are generated alongside the view and owned-view wrapper (and are therefore gated with them). Hand-written implementations are only needed for hand-written view types and must follow the same shape.

Required Associated Types§

Source

type View<'a>: MessageView<'a, Owned = Self> + Send + Sync

The zero-copy view of Self, borrowing from a buffer with lifetime 'a.

Source

type ViewHandle: From<OwnedView<Self::View<'static>>> + AsRef<OwnedView<Self::View<'static>>> + Send + Sync + 'static

The generated 'static owned-view handle for Self (FooOwnedView).

Provided Methods§

Source

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

Decode a borrowed View from a byte slice.

Convenience for generic code: lets a caller bounded only on M: HasMessageView write M::decode_view(buf) instead of the associated-type path <M as HasMessageView>::View::decode_view(buf). The returned view borrows from buf. Reading the returned view (e.g. to_owned_message) requires MessageView in scope.

Like the underlying MessageView::decode_view, this does not enforce max_message_size; use decode_view_with_options for that.

§Errors

Returns DecodeError if the buffer contains invalid protobuf data.

Source

fn decode_view_with_options<'a>( buf: &'a [u8], opts: &DecodeOptions, ) -> Result<Self::View<'a>, DecodeError>

Decode a borrowed View under custom DecodeOptions (recursion limit, max message size, unknown-field limit).

Convenience for generic code; equivalent to DecodeOptions::decode_view::<M::View<'_>>.

§Errors

Returns DecodeError if the buffer is invalid or exceeds the configured limits.

Source

fn decode_view_handle(bytes: Bytes) -> Result<Self::ViewHandle, DecodeError>

Decode a ViewHandle from a Bytes buffer.

Convenience for generic code; equivalent to decoding an OwnedView<Self::View<'static>> and converting it with From.

§Errors

Returns DecodeError if the buffer contains invalid protobuf data.

Source

fn decode_view_handle_with_options( bytes: Bytes, opts: &DecodeOptions, ) -> Result<Self::ViewHandle, DecodeError>

Decode a ViewHandle with custom DecodeOptions (recursion limit, max message size).

§Errors

Returns DecodeError if the buffer is invalid or exceeds the configured limits.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§