Skip to main content

Storage

Trait Storage 

Source
pub unsafe trait Storage:
    Sized
    + Deref
    + 'static { }
Expand description

Implemented for types that can safely provide a stable, aliasable reference to data they own.

§noalias

The pointers behind common allocation types (Box<T>, Vec<T>, etc), are stored via core::ptr::Unique<T>, which passes to the compilier a noalias attribute. This attribute allows the compiler to make optimisations with the guarantee that no other pointers are referencing the same data.

We want to both own the data and provide a reference to it, outside of Rust’s normal lifetime guarantees, which can break with some of the optimisations the compiler can make. To achieve this, we need to remove the noalias attribute of the underlying pointer to let the compiler know that there will exist multiple pointers referencing the same owned data, which is also known as aliasing.

§Safety

The implementer must guarantee that the reference it provides via Deref will be both stable and aliasable for the lifetime of self. Stable in this context meaning that the pointer to the data referenced will not change.

Box<T> provides a stable pointer (the location of the data being pointed to will not change) but is not aliasable (see noalias above). Instead we can use the basic wrapper types provided by the aliasable crate.

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.

Implementations on Foreign Types§

Source§

impl Storage for AliasableString

Source§

impl<T: 'static> Storage for AliasableVec<T>

Source§

impl<T: ?Sized + 'static> Storage for AliasableBox<T>

Implementors§