use crate::io::Completion; #[cfg(feature = "fs")] use crate::io::File; use anyhow::Result; use std::rc::Rc; pub struct PageSource { io: Rc, } impl PageSource { pub fn from_io(io: Rc) -> Self { Self { io } } #[cfg(feature = "fs")] pub fn from_file(file: Box) -> Self { Self { io: Rc::new(FileStorage::new(file)), } } pub fn get(&self, page_idx: usize, c: Rc) -> Result<()> { self.io.get(page_idx, c) } } pub trait PageIO { fn get(&self, page_idx: usize, c: Rc) -> Result<()>; } #[cfg(feature = "fs")] struct FileStorage { file: Box, } #[cfg(feature = "fs")] impl PageIO for FileStorage { fn get(&self, page_idx: usize, c: Rc) -> Result<()> { let page_size = c.buf().len(); assert!(page_idx > 0); assert!(page_size >= 512); assert!(page_size <= 65536); assert!((page_size & (page_size - 1)) == 0); let pos = (page_idx - 1) * page_size; self.file.pread(pos, c)?; Ok(()) } } #[cfg(feature = "fs")] impl FileStorage { pub fn new(file: Box) -> Self { Self { file } } }