Skip to content

DefaultViewInstance is not actually unsafe #69

@techninja1008

Description

@techninja1008

buffa/buffa/src/view.rs

Lines 200 to 226 in 3f80d56

/// Provides access to a lazily-initialized default view instance.
///
/// View types implement this trait so that [`MessageFieldView`] can
/// dereference to a default when unset, just as [`MessageField`](crate::MessageField)
/// does for owned types via [`DefaultInstance`](crate::DefaultInstance).
///
/// Generated view types like `FooView<'a>` contain only covariant borrows
/// (`&'a str`, `&'a [u8]`, etc.). A default view contains only `'static`
/// data (`""`, `&[]`, `0`), so a `&'static FooView<'static>` can be safely
/// reinterpreted as `&'static FooView<'a>` for any `'a` via covariance.
///
/// This trait is implemented for the `'static` instantiation (e.g.,
/// `FooView<'static>`) and the [`MessageFieldView`] `Deref` impl uses
/// a lifetime transmute to serve it for any `'a`.
///
/// # Safety
///
/// Implementors must ensure:
/// 1. The returned reference points to a validly-initialized default
/// instance in a `'static` location that is never mutated.
/// 2. The type is **covariant** in all lifetime parameters — the default
/// `'static` instance must be safely reinterpretable at any shorter
/// lifetime.
pub unsafe trait DefaultViewInstance: Default + 'static {
/// Return a reference to the single default view instance.
fn default_view_instance() -> &'static Self;
}

Similar to #68, the DefaultViewInstance trait is not actually unsafe. Both safety invariants that it tries to protect from are already handled by rust's type system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions