use crate::linear_object_pool::LinearObjectPool;
use crate::page::{Page, PageId};
use std::ops::{Deref, DerefMut};
pub struct LinearReusable<'a, T> {
pool: &'a LinearObjectPool<T>,
page_id: PageId,
page: &'a Page<T>,
}
impl<'a, T> LinearReusable<'a, T> {
#[inline]
pub(crate) unsafe fn new(
pool: &'a LinearObjectPool<T>,
page_id: PageId,
page: &'a Page<T>,
) -> Self {
Self {
pool,
page_id,
page,
}
}
}
impl<'a, T> DerefMut for LinearReusable<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
self.page.get_mut(&self.page_id)
}
}
}
impl<'a, T> Deref for LinearReusable<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe {
self.page.get(&self.page_id)
}
}
}
impl<'a, T> Drop for LinearReusable<'a, T> {
#[inline]
fn drop(&mut self) {
let page = self.page;
(self.pool.get_reset_callback())(unsafe {
page.get_mut(&self.page_id)
});
page.free(&self.page_id);
}
}