Trait feanor_math::mempool::MemoryProvider
source · pub trait MemoryProvider<T> {
type Object: Deref<Target = [T]> + DerefMut + VectorViewMut<T>;
// Required method
unsafe fn get_new<F: FnOnce(&mut [MaybeUninit<T>])>(
&self,
size: usize,
initializer: F,
) -> Self::Object;
// Provided methods
fn get_new_init<F: FnMut(usize) -> T>(
&self,
size: usize,
initializer: F,
) -> Self::Object { ... }
fn try_get_new_init<E, F: FnMut(usize) -> Result<T, E>>(
&self,
size: usize,
initializer: F,
) -> Result<Self::Object, E> { ... }
}Expand description
Trait for objects that can provide memory, often for short-term use.
This includes naive implementations like AllocatingMemoryProvider that
just allocate memory, or alternatively all kinds of memory pools and recyclers
(e.g. caching::CachingMemoryProvider).
This is related to std::alloc::Allocator, but less restrictive, as it may
return objects with certain structure. In particular, it naturally allows e.g.
memory pools or memory recycling.
It is usually used when certain objects or algorithms need frequent allocations
(often all of the same size), either because they need temporary, internal memory,
or they represent rings and have to allocate memory for elements. On the other hand,
if a struct just needs to store some data during its lifetime, memory pooling is
usually not useful, and a standard Vec is often used instead.