Skip to content

Commit

Permalink
Add BlockDevice trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Aug 6, 2024
1 parent 95861f8 commit 9e6d90e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,30 @@ pub trait Storage: ReadStorage {
/// and might as such do RMW operations at an undesirable performance impact.
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>;
}

/// The size in bytes of a single block read or written by a [`BlockDevice`].
pub const BLOCK_SIZE: usize = 512;

/// A single block which may be read or written by a [`BlockDevice`].
///
/// Also referred to as a sector in some contexts.
pub type Block = [u8; BLOCK_SIZE];

/// A device which can read and write whole numbers of blocks.
pub trait BlockDevice {
/// The error type returned by methods on this trait.
type Error;

/// Returns the size of the device in blocks.
fn block_count(&self) -> Result<usize, Self::Error>;

/// Reads some number of blocks from the device, starting at `first_block_index`.
///
/// `first_block_index + blocks.len()` must not be greater than the size returned by `block_count`.
fn read(&mut self, first_block_index: u64, blocks: &mut [Block]) -> Result<(), Self::Error>;

/// Writes some number of blocks to the device, starting at `first_block_index`.
///
/// `first_block_index + blocks.len()` must not be greater than the size returned by `block_count`.
fn write(&mut self, first_block_index: u64, blocks: &[Block]) -> Result<(), Self::Error>;
}

0 comments on commit 9e6d90e

Please sign in to comment.