use async_trait::async_trait; use tokio::fs::File; use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom}; use crate::store::{FilePosition, Result}; #[async_trait] pub(crate) trait CursorCanRead { fn file(&mut self) -> &mut File; fn eof_file_position(&self) -> FilePosition; async fn read_bytes(&mut self, bytes: &mut [u8]) -> Result<()> { self.file().read_exact(bytes).await?; Ok(()) } async fn get_bytes(&mut self, count: usize) -> Result> { let mut result: Vec = Vec::with_capacity(count); self.read_bytes(&mut result).await?; Ok(result) } async fn seek_to(&mut self, file_position: FilePosition) -> Result { let file_position = self.file().seek(SeekFrom::Start(file_position)).await?; Ok(file_position) } // Start of the file i.e. the Header, not the entries. async fn seek_to_start(&mut self) -> Result { let file_position = self.file().seek(SeekFrom::Start(0)).await?; Ok(file_position) } async fn seek_to_end(&mut self) -> Result { let file_position = self.file().seek(SeekFrom::End(0)).await?; Ok(file_position) } // Seeks from current position by offset and returns new file position async fn seek_by(&mut self, offset: i64) -> Result { let file_position = self.file().seek(SeekFrom::Current(offset)).await?; Ok(file_position) } async fn current_file_position(&mut self) -> Result { let next_file_position: FilePosition = self.file().stream_position().await?; Ok(next_file_position) } async fn is_at_eof(&mut self) -> Result { let current_file_position = self.current_file_position().await?; let eof_file_position = self.eof_file_position(); Ok(current_file_position == eof_file_position) } } #[async_trait] pub(crate) trait CursorCanWrite: CursorCanRead { async fn write_bytes(&mut self, bytes: &[u8]) -> Result { Ok(self.file().write(bytes).await?) } }