Skip to main content

HasMessageView

Trait HasMessageView 

Source
pub trait HasMessageView: Message + Sized {
    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>
       where Self::View<'static>: ViewLifetimeParametric { ... }
    fn decode_view_handle_with_options(
        bytes: Bytes,
        opts: &DecodeOptions,
    ) -> Result<Self::ViewHandle, DecodeError>
       where Self::View<'static>: ViewLifetimeParametric { ... }
}
Expand description

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 reaches the view through the handle (handle.as_ref().reborrow() or .to_owned_message()) adds M::View<'static>: ViewReborrow as a bound at the use site, and generic code that decodes a handle (decode_view_handle) adds M::View<'static>: ViewLifetimeParametric, which implies the former; every generated view satisfies both. (Neither bound can 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). Neither is generated for messages selected by borrowed_views_only_in or for cleanup (zeroize_in, generate_zeroize), nor for messages that contain them; own their views with buffa::sensitive::GuardedOwnedView instead. 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 or exceeds a default recursion, unknown-field, or element-memory limit.

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, and element-memory 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>
where Self::View<'static>: ViewLifetimeParametric,

Decode a ViewHandle from a Bytes buffer.

Convenience for generic code; equivalent to decoding an OwnedView<Self::View<'static>> and converting it with From. Like decode_view, this does not enforce max_message_size; use decode_view_handle_with_options for that.

§Errors

Returns DecodeError if the buffer contains invalid protobuf data or exceeds a default recursion, unknown-field, or element-memory limit.

Source

fn decode_view_handle_with_options( bytes: Bytes, opts: &DecodeOptions, ) -> Result<Self::ViewHandle, DecodeError>
where Self::View<'static>: ViewLifetimeParametric,

Decode a ViewHandle with custom DecodeOptions (recursion limit, max message size, unknown-field limit, and element-memory limit).

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