pub trait Storage {
type Stored<T>: DerefMut<Target = T> + From<T>;
// Required method
fn name() -> &'static str ⓘ;
}Expand description
🫗 Allows data structures to be generic over their storage strategy.
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 inline (stack-allocated).
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§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".