Skip to main content

ViewReborrow

Trait ViewReborrow 

Source
pub trait ViewReborrow: MessageView<'static> {
    type Reborrowed<'b>: MessageView<'b, Owned = <Self as MessageView<'static>>::Owned>
       where Self: 'b;

    // Required method
    fn reborrow<'b>(this: &'b Self) -> &'b Self::Reborrowed<'b>;
}
Expand description

Exposes the real lifetime of an OwnedView’s borrows.

OwnedView<V> stores V with a 'static lifetime — the actual borrows point into its internal Bytes buffer. ViewReborrow lets OwnedView::reborrow return a reference typed as &'b V::Reborrowed<'b>, tying the borrow to &'b self so the compiler can reason about it correctly.

Codegen emits impl ViewReborrow automatically for every generated view type. Hand-written view types must provide it manually if OwnedView::reborrow is needed.

§Soundness

ViewReborrow is a safe trait. Soundness is established mechanically by the compiler at each impl site: the reborrow method body coerces a &'b Self (where Self = FooView<'static>) to &'b Self::Reborrowed<'b> (= &'b FooView<'b>). Rust accepts this only when FooView is covariant in its lifetime parameter — a covariant FooView<'static> is a subtype of FooView<'b> and the coercion is a standard subtyping move. Invariant fields (Cell<&'a T>, &'a mut T, fn(&'a T)) make the type invariant in 'a; the trait body then fails to compile and the impl is rejected — which is exactly what should happen, because narrowing the lifetime of an invariant view would be unsound.

Hand-written impls cannot accidentally introduce undefined behaviour without writing unsafe themselves: the canonical body is just this, which the type checker accepts iff the variance permits the coercion.

Required Associated Types§

Source

type Reborrowed<'b>: MessageView<'b, Owned = <Self as MessageView<'static>>::Owned> where Self: 'b

The same view type with its lifetime shortened to 'b.

Required Methods§

Source

fn reborrow<'b>(this: &'b Self) -> &'b Self::Reborrowed<'b>

Coerce &'b Self (= &'b FooView<'static>) to &'b Self::Reborrowed<'b> (= &'b FooView<'b>). The canonical body is just this; the compiler accepts it via standard lifetime variance for covariant view types.

Called by OwnedView::reborrow; users shouldn’t need to call this method directly.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§