use std::mem::MaybeUninit;
use std::path::Path;
use doublets::mem::{FileMapped, RawMem, Result as MemResult};
#[derive(Debug)]
pub struct PersistentFileMapped<T>(FileMapped<T>);
impl<T> PersistentFileMapped<T> {
pub fn from_path<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
FileMapped::from_path(path).map(Self)
}
pub fn new(file: std::fs::File) -> std::io::Result<Self> {
FileMapped::new(file).map(Self)
}
pub fn inner(&self) -> &FileMapped<T> {
&self.0
}
}
impl<T> RawMem for PersistentFileMapped<T> {
type Item = T;
fn allocated(&self) -> &[Self::Item] {
self.0.allocated()
}
fn allocated_mut(&mut self) -> &mut [Self::Item] {
self.0.allocated_mut()
}
unsafe fn grow(
&mut self,
addition: usize,
fill: impl FnOnce(usize, (&mut [Self::Item], &mut [MaybeUninit<Self::Item>])),
) -> MemResult<&mut [Self::Item]> {
unsafe { self.0.grow(addition, fill) }
}
fn shrink(&mut self, cap: usize) -> MemResult<()> {
self.0.shrink(cap)
}
fn grow_filled(&mut self, cap: usize, value: Self::Item) -> MemResult<&mut [Self::Item]>
where
Self::Item: Clone,
{
unsafe { self.0.grow_filled_exact(cap, value) }
}
}