use crate::platform::*;
pub struct ThreadLocalPool<T> {
pool: UnsafeCell<Vec<T>>,
max_size: usize,
_marker: PhantomData<*const T>,
}
impl<T> ThreadLocalPool<T> {
#[must_use]
pub fn new(max_size: usize) -> Self {
Self {
pool: UnsafeCell::new(Vec::with_capacity(max_size)),
max_size,
_marker: PhantomData,
}
}
pub fn get_or_create<F>(&self, create: F) -> T
where
F: FnOnce() -> T,
{
unsafe {
let pool = &mut *self.pool.get();
pool.pop().unwrap_or_else(create)
}
}
pub fn put(&self, obj: T) {
unsafe {
let pool = &mut *self.pool.get();
if pool.len() < self.max_size {
pool.push(obj);
}
}
}
}