Skip to main content

View

Trait View 

Source
pub unsafe trait View<Source> {
    type ViewType<'a>
       where Source: 'a;
    type StaticViewType: 'static;

    // Required method
    fn view(source: &Source) -> Self::ViewType<'_>;
}
Expand description

Associates a source type with a projected view via a marker.

ViewType<'a> is the real view with borrowed fields. StaticViewType is the same struct with 'static — used only for step resolution at build time, never observed at runtime.

§Examples

use nexus_rt::View;

struct OrderView<'a> { symbol: &'a str, qty: u64 }
struct NewOrder { symbol: String, qty: u64 }

struct AsOrderView;
unsafe impl View<NewOrder> for AsOrderView {
    type ViewType<'a> = OrderView<'a>;
    type StaticViewType = OrderView<'static>;
    fn view(source: &NewOrder) -> OrderView<'_> {
        OrderView { symbol: &source.symbol, qty: source.qty }
    }
}

§Safety

StaticViewType must be layout-identical to ViewType<'a> for any 'a. They must be the same struct with different lifetime parameters. The framework performs a pointer cast between them in with_view().

For view structs with borrowed fields (e.g., &'a str), use #[repr(C)] to guarantee layout stability across lifetime parameters. Rust does not currently guarantee that repr(Rust) types with different lifetime parameters have identical layouts, though all current compilers do so. #[repr(C)] removes any theoretical risk.

Incorrect implementations (e.g., StaticViewType being a different struct) cause undefined behavior. Use the #[derive(View)] macro when available to generate correct implementations.

Required Associated Types§

Source

type ViewType<'a> where Source: 'a

The view type with the source borrow lifetime.

Source

type StaticViewType: 'static

The same type with 'static — for IntoRefStep trait resolution. Must be layout-identical to ViewType<'a> for any 'a.

Required Methods§

Source

fn view(source: &Source) -> Self::ViewType<'_>

Construct the view from a borrowed source.

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§