Trait devela::mem::Storage

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

    // Required method
    fn name() -> &'static str;
}
Available on crate feature mem only.
Expand description

Allows to be generic in respect of the data storage.

There are two reference implementations:

Examples

use core::{array, mem::size_of};
use devela::mem::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, S: Storage, const L: usize> MyStructure<T, S, L> {
    pub fn new() -> Self
    where
        T: Default,
    {
        Self {
            data: S::Stored::from(array::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::mem::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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Storage for ()

A storage type that wraps its data in a Direct.

§

type Stored<T> = Direct<T>

source§

fn name() -> &'static str

Implementors§

source§

impl Storage for Boxed

Available on crate feature alloc only.
§

type Stored<T> = Box<T>