use crate::error::MappedPageError;
use crate::page::{MappedPage, PageId};
use crate::pager::Pager;
use crate::protected::{ProtectedPageId, ProtectedPageWriter};
pub trait PageHandle<A: ?Sized> {
type Error;
type Mut<'a>: 'a
where
A: 'a,
Self: 'a;
fn get<'a>(&self, allocator: &'a A) -> Result<&'a MappedPage, Self::Error>;
fn get_mut<'a>(&self, allocator: &'a mut A) -> Result<Self::Mut<'a>, Self::Error>;
}
pub trait PageAllocator<AllocatedType: PageHandle<Self>>: Sized {
type Error;
fn alloc(&mut self) -> Result<AllocatedType, Self::Error>;
fn free(&mut self, id: AllocatedType) -> Result<(), Self::Error>;
}
pub trait BulkPageAllocator<AllocatedType: PageHandle<Self>>: PageAllocator<AllocatedType> {
fn alloc_bulk(&mut self, count: usize) -> Result<Vec<AllocatedType>, Self::Error>;
fn free_bulk(&mut self, ids: Vec<AllocatedType>) -> Result<(), Self::Error>;
}
impl<const PAGE_SIZE: usize> PageHandle<Pager<PAGE_SIZE>> for PageId<PAGE_SIZE> {
type Error = MappedPageError;
type Mut<'a> = &'a mut MappedPage;
fn get<'a>(&self, pager: &'a Pager<PAGE_SIZE>) -> Result<&'a MappedPage, MappedPageError> {
pager.get_page(*self)
}
fn get_mut<'a>(
&self,
pager: &'a mut Pager<PAGE_SIZE>,
) -> Result<&'a mut MappedPage, MappedPageError> {
pager.get_page_mut(*self)
}
}
impl<const PAGE_SIZE: usize> PageAllocator<PageId<PAGE_SIZE>> for Pager<PAGE_SIZE> {
type Error = MappedPageError;
fn alloc(&mut self) -> Result<PageId<PAGE_SIZE>, MappedPageError> {
Pager::alloc(self)
}
fn free(&mut self, id: PageId<PAGE_SIZE>) -> Result<(), MappedPageError> {
Pager::free(self, id)
}
}
impl<const PAGE_SIZE: usize> PageHandle<Pager<PAGE_SIZE>> for ProtectedPageId<PAGE_SIZE> {
type Error = MappedPageError;
type Mut<'a> = ProtectedPageWriter<'a, PAGE_SIZE>;
fn get<'a>(&self, pager: &'a Pager<PAGE_SIZE>) -> Result<&'a MappedPage, MappedPageError> {
pager.get_protected_page(*self)
}
fn get_mut<'a>(
&self,
pager: &'a mut Pager<PAGE_SIZE>,
) -> Result<ProtectedPageWriter<'a, PAGE_SIZE>, MappedPageError> {
pager.get_protected_page_mut(*self)
}
}
impl<const PAGE_SIZE: usize> PageAllocator<ProtectedPageId<PAGE_SIZE>> for Pager<PAGE_SIZE> {
type Error = MappedPageError;
fn alloc(&mut self) -> Result<ProtectedPageId<PAGE_SIZE>, MappedPageError> {
Pager::alloc_protected(self)
}
fn free(&mut self, id: ProtectedPageId<PAGE_SIZE>) -> Result<(), MappedPageError> {
Pager::free_protected(self, id)
}
}
impl<const PAGE_SIZE: usize> BulkPageAllocator<PageId<PAGE_SIZE>> for Pager<PAGE_SIZE> {
fn alloc_bulk(&mut self, count: usize) -> Result<Vec<PageId<PAGE_SIZE>>, MappedPageError> {
Pager::alloc_bulk(self, count)
}
fn free_bulk(&mut self, ids: Vec<PageId<PAGE_SIZE>>) -> Result<(), MappedPageError> {
Pager::free_bulk(self, ids)
}
}
impl<const PAGE_SIZE: usize> BulkPageAllocator<ProtectedPageId<PAGE_SIZE>> for Pager<PAGE_SIZE> {
fn alloc_bulk(
&mut self,
count: usize,
) -> Result<Vec<ProtectedPageId<PAGE_SIZE>>, MappedPageError> {
Pager::alloc_protected_bulk(self, count)
}
fn free_bulk(&mut self, ids: Vec<ProtectedPageId<PAGE_SIZE>>) -> Result<(), MappedPageError> {
Pager::free_protected_bulk(self, ids)
}
}