devela::sys::mem

Trait Storage

Source
pub trait Storage {
    type Stored<T>: DerefMut<Target = T> + From<T>;

    // Required method
    fn name() -> &'static str ;
}
Expand description

Allows to be generic in respect of the data storage.

There are two reference implementations:

§Examples

use core::array::from_fn;
use devela::Storage;

/// Generically store a generic array of generic size.
pub struct MyStructure<T, S: Storage, const L: usize> {
    data: S::Stored<[T; L]>,
}

impl<T: Default, S: Storage, const L: usize> MyStructure<T, S, L> {
    pub fn new() -> Self {
        Self {
            data: S::Stored::from(from_fn(|_| T::default())),
        }
    }
}

// The array is stored in the stack
assert_eq![100, size_of::<MyStructure::<u8, (), 100>>()];

// The array is stored in the heap.
#[cfg(feature = "alloc")]
assert_eq![8, size_of::<MyStructure::<u8, devela::Boxed, 100>>()];

Required Associated Types§

Source

type Stored<T>: DerefMut<Target = T> + From<T>

The stored associated type.

Any type T that is to be stored must be able to be dereferenced to a mutable reference of T and to be constructed from a value of type T.

Required Methods§

Source

fn name() -> &'static str

Returns the static name of the storage implementation.

This can be useful for debugging.

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 Boxed

Available on crate feature alloc only.

This implementation is equivalent to the one for Bare which uses BareBox for storage.

Source§

type Stored<T> = Box<T>

Source§

impl Storage for Bare

A zero-sized marker for a storage type that wraps its data in a BareBox.

This implementation is equivalent to the one for Boxed which uses Box for storage.

Source§

type Stored<T> = BareBox<T>