use crate::MappedPageError;
use crate::page::MappedPage;
use crate::pager::Pager;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ProtectedPageId<const PAGE_SIZE: usize>(pub u64);
impl<const PAGE_SIZE: usize> ProtectedPageId<PAGE_SIZE> {
pub fn get<'a>(&self, pager: &'a Pager<PAGE_SIZE>) -> Result<&'a MappedPage, MappedPageError> {
pager.get_protected_page(*self)
}
pub fn get_mut<'a>(
&self,
pager: &'a mut Pager<PAGE_SIZE>,
) -> Result<ProtectedPageWriter<'a, PAGE_SIZE>, MappedPageError> {
pager.get_protected_page_mut(*self)
}
}
pub struct ProtectedPageWriter<'a, const PAGE_SIZE: usize> {
pub(crate) pager: &'a mut Pager<PAGE_SIZE>,
pub(crate) id: ProtectedPageId<PAGE_SIZE>,
pub(crate) inactive_phys_page: u64,
pub(crate) inactive_slot: u8,
}
impl<'a, const PAGE_SIZE: usize> ProtectedPageWriter<'a, PAGE_SIZE> {
pub fn page_mut(&mut self) -> &mut MappedPage {
let off = self.inactive_phys_page as usize * PAGE_SIZE;
let slice = &mut self
.pager
.mmap
.as_mut()
.expect("mmap available: verified at ProtectedPageWriter construction")
[off..off + PAGE_SIZE];
unsafe { MappedPage::from_slice_mut(slice) }
}
pub fn commit(self) -> Result<(), MappedPageError> {
self.pager
.commit_protected_write(self.id, self.inactive_phys_page, self.inactive_slot)
}
}