Skip to content

Commit

Permalink
Make block size a parameter of BlockDevice trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Aug 7, 2024
1 parent 1d508c6 commit bf3e225
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,10 @@ pub trait Storage: ReadStorage {
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 {
///
/// Blocks are also referred to as sectors in some contexts.
pub trait BlockDevice<const BLOCK_SIZE: usize = 512> {
/// The error type returned by methods on this trait.
type Error;

Expand All @@ -74,11 +68,19 @@ pub trait BlockDevice {
///
/// `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>;
fn read(
&mut self,
first_block_index: u64,
blocks: &mut [[u8; BLOCK_SIZE]],
) -> 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>;
fn write(
&mut self,
first_block_index: u64,
blocks: &[[u8; BLOCK_SIZE]],
) -> Result<(), Self::Error>;
}

0 comments on commit bf3e225

Please sign in to comment.