pub trait FileIo{
// Required methods
fn open(path: &Path) -> Result<Self>;
fn open_existing(path: &Path) -> Result<Self>;
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>;
fn write_at(&mut self, offset: u64, data: &[u8]) -> Result<()>;
fn sync(&mut self) -> Result<()>;
fn len(&self) -> Result<u64>;
fn set_len(&mut self, len: u64) -> Result<()>;
}Expand description
Positional, synchronous file IO.
§Why a trait
- Lets tests substitute fault-injecting or in-memory implementations.
- Lets integrators add use-case specific behavior (write-through caching, custom placement, etc.) without forking the crate.
All reads and writes are positional (pread/pwrite style) so a single
handle can serve concurrent readers without a shared cursor. write_at
does not flush: callers that need durability must call sync, which is
what lets the log writer batch many writes under one fsync.
Required Methods§
Sourcefn open_existing(path: &Path) -> Result<Self>
fn open_existing(path: &Path) -> Result<Self>
Opens an existing file; errors if it does not exist. Read and recovery paths use this so they can never create stray files.
Sourcefn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>
Read exactly buf.len() bytes at offset. Errors if the file is too short.
Sourcefn write_at(&mut self, offset: u64, data: &[u8]) -> Result<()>
fn write_at(&mut self, offset: u64, data: &[u8]) -> Result<()>
Write all of data at offset. Durability requires a subsequent sync.
Sourcefn sync(&mut self) -> Result<()>
fn sync(&mut self) -> Result<()>
Flush written data (and the metadata needed to read it back) to disk.
fn len(&self) -> Result<u64>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".