62 lines
2.1 KiB
Rust
62 lines
2.1 KiB
Rust
use async_trait::async_trait;
|
|
use tokio::fs::File;
|
|
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom};
|
|
|
|
use crate::store::{FilePosition, Result};
|
|
|
|
#[async_trait]
|
|
pub trait CursorCanRead<T> {
|
|
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<Vec<u8>> {
|
|
let mut result: Vec<u8> = Vec::with_capacity(count);
|
|
self.read_bytes(&mut result).await?;
|
|
Ok(result)
|
|
}
|
|
|
|
async fn seek_to(&mut self, file_position: FilePosition) -> Result<FilePosition> {
|
|
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<FilePosition> {
|
|
let file_position = self.file().seek(SeekFrom::Start(0)).await?;
|
|
Ok(file_position)
|
|
}
|
|
|
|
async fn seek_to_end(&mut self) -> Result<FilePosition> {
|
|
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<FilePosition> {
|
|
let file_position = self.file().seek(SeekFrom::Current(offset)).await?;
|
|
Ok(file_position)
|
|
}
|
|
|
|
async fn current_file_position(&mut self) -> Result<FilePosition> {
|
|
let next_file_position: FilePosition = self.file().stream_position().await?;
|
|
Ok(next_file_position)
|
|
}
|
|
|
|
async fn is_at_eof(&mut self) -> Result<bool> {
|
|
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 trait CursorCanWrite<T>: CursorCanRead<T> {
|
|
async fn write_bytes(&mut self, bytes: &[u8]) -> Result<usize> {
|
|
Ok(self.file().write(bytes).await?)
|
|
}
|
|
}
|