Skip to main content

FileIo

Trait FileIo 

Source
pub trait FileIo
where Self: Sized + Send + Sync + Debug,
{ // 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

  1. Lets tests substitute fault-injecting or in-memory implementations.
  2. 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§

Source

fn open(path: &Path) -> Result<Self>

Opens a file for writing, creating it if missing.

Source

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.

Source

fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>

Read exactly buf.len() bytes at offset. Errors if the file is too short.

Source

fn write_at(&mut self, offset: u64, data: &[u8]) -> Result<()>

Write all of data at offset. Durability requires a subsequent sync.

Source

fn sync(&mut self) -> Result<()>

Flush written data (and the metadata needed to read it back) to disk.

Source

fn len(&self) -> Result<u64>

Source

fn set_len(&mut self, len: u64) -> Result<()>

Changes the physical file length. The writer uses growth as sparse preallocation and shrinkage to remove allocation tails during recovery and sealing.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§