ext4_lwext4/blockdev/traits.rs
1//! Block device trait definition.
2
3use crate::error::Result;
4
5/// Trait for block devices that can be used with ext4 filesystems.
6///
7/// Implementors must provide block-level read/write operations.
8/// All operations are performed on aligned blocks.
9pub trait BlockDevice: Send {
10 /// Read blocks from the device.
11 ///
12 /// # Arguments
13 /// * `block_id` - Starting block number
14 /// * `buf` - Buffer to read into (must be block_size * block_count bytes)
15 ///
16 /// # Returns
17 /// Number of blocks read, or error
18 fn read_blocks(&self, block_id: u64, buf: &mut [u8]) -> Result<u32>;
19
20 /// Write blocks to the device.
21 ///
22 /// # Arguments
23 /// * `block_id` - Starting block number
24 /// * `buf` - Buffer to write from (must be block_size * block_count bytes)
25 ///
26 /// # Returns
27 /// Number of blocks written, or error
28 fn write_blocks(&mut self, block_id: u64, buf: &[u8]) -> Result<u32>;
29
30 /// Flush any pending writes to the device.
31 fn flush(&mut self) -> Result<()>;
32
33 /// Get the physical block size in bytes.
34 fn block_size(&self) -> u32;
35
36 /// Get the total number of blocks.
37 fn block_count(&self) -> u64;
38
39 /// Open the device (called before first I/O operation).
40 ///
41 /// Default implementation does nothing.
42 fn open(&mut self) -> Result<()> {
43 Ok(())
44 }
45
46 /// Close the device (called when unmounting).
47 ///
48 /// Default implementation does nothing.
49 fn close(&mut self) -> Result<()> {
50 Ok(())
51 }
52}
53
54/// Extension trait for BlockDevice with helper methods.
55pub trait BlockDeviceExt: BlockDevice {
56 /// Get the total size of the device in bytes.
57 fn total_size(&self) -> u64 {
58 self.block_count() * self.block_size() as u64
59 }
60}
61
62impl<T: BlockDevice> BlockDeviceExt for T {}