Skip to content

Commit 1d508c6

Browse files
committed
Add BlockDevice trait.
1 parent 95861f8 commit 1d508c6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ pub trait Storage: ReadStorage {
5353
/// and might as such do RMW operations at an undesirable performance impact.
5454
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>;
5555
}
56+
57+
/// The size in bytes of a single block read or written by a [`BlockDevice`].
58+
pub const BLOCK_SIZE: usize = 512;
59+
60+
/// A single block which may be read or written by a [`BlockDevice`].
61+
///
62+
/// Also referred to as a sector in some contexts.
63+
pub type Block = [u8; BLOCK_SIZE];
64+
65+
/// A device which can read and write whole numbers of blocks.
66+
pub trait BlockDevice {
67+
/// The error type returned by methods on this trait.
68+
type Error;
69+
70+
/// Returns the size of the device in blocks.
71+
fn block_count(&self) -> Result<usize, Self::Error>;
72+
73+
/// Reads some number of blocks from the device, starting at `first_block_index`.
74+
///
75+
/// `first_block_index + blocks.len()` must not be greater than the size returned by
76+
/// `block_count`.
77+
fn read(&mut self, first_block_index: u64, blocks: &mut [Block]) -> Result<(), Self::Error>;
78+
79+
/// Writes some number of blocks to the device, starting at `first_block_index`.
80+
///
81+
/// `first_block_index + blocks.len()` must not be greater than the size returned by
82+
/// `block_count`.
83+
fn write(&mut self, first_block_index: u64, blocks: &[Block]) -> Result<(), Self::Error>;
84+
}

0 commit comments

Comments
 (0)