Storage

Trait Storage 

Source
pub unsafe trait Storage: Sized {
    // Required methods
    fn new<T>(data: T) -> Self;
    fn ptr(&self) -> NonNull<()>;
    fn ptr_mut(&mut self) -> NonNull<()>;
    unsafe fn drop_in_place(&mut self, layout: Layout);

    // Provided methods
    unsafe fn as_ref<T>(&self) -> &T { ... }
    unsafe fn as_mut<T>(&mut self) -> &mut T { ... }
    unsafe fn as_pinned_mut<T>(self: Pin<&mut Self>) -> Pin<&mut T> { ... }
}
Expand description

A storage that can be used to store dynamic type-erased objects.

§Safety

ptr/ptr_mut/as_ref/as_mut/as_pinned_mut must return a pointer/reference to stored data.

Required Methods§

Source

fn new<T>(data: T) -> Self

Constructs a new storage storing T.

Source

fn ptr(&self) -> NonNull<()>

Returns a const pointer to stored data.

Source

fn ptr_mut(&mut self) -> NonNull<()>

Returns a mutable pointer to stored data.

Source

unsafe fn drop_in_place(&mut self, layout: Layout)

Drop the storage in place with the layout of the stored data.

Stored data should have been dropped in place before calling this method.

§Safety

drop_in_place must be called once, and the storage must not be used after. layout must be the layout of the data stored.

Provided Methods§

Source

unsafe fn as_ref<T>(&self) -> &T

Returns a reference to stored data.

§Safety

Storage must have been constructed with T

Source

unsafe fn as_mut<T>(&mut self) -> &mut T

Returns a mutable reference to stored data.

§Safety

Storage must have been constructed with T

Source

unsafe fn as_pinned_mut<T>(self: Pin<&mut Self>) -> Pin<&mut T>

Returns a pinned mutable reference to stored data.

§Safety

Storage must have been constructed with from T

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§

Source§

impl Storage for Box

Available on crate feature alloc only.
Source§

impl<const SIZE: usize, const ALIGN: usize> Storage for Raw<SIZE, ALIGN>
where Align<ALIGN>: Alignment,

Source§

impl<const SIZE: usize, const ALIGN: usize> Storage for RawOrBox<SIZE, ALIGN>
where Align<ALIGN>: Alignment,