diff --git a/src/lib.rs b/src/lib.rs index 3070723..a9303c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,3 +53,32 @@ 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; + + /// 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>; +}