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§
Required Methods§
Object Safety§
This trait is not object safe.