1pub const BLOCK_SIZE: usize = 512;
3
4pub trait BlockDevice {
7 type Error;
9
10 async fn read_block(
12 &mut self,
13 block: u32,
14 buffer: &mut [u8; BLOCK_SIZE],
15 ) -> Result<(), Self::Error>;
16
17 async fn num_blocks(&mut self) -> Result<u32, Self::Error>;
19}
20
21#[cfg(feature = "write")]
25pub trait BlockWrite: BlockDevice {
26 async fn write_block(
29 &mut self,
30 block: u32,
31 buffer: &[u8; BLOCK_SIZE],
32 ) -> Result<(), Self::Error>;
33}
34
35#[cfg(feature = "write")]
36impl<D: BlockWrite + ?Sized> BlockWrite for &mut D {
37 async fn write_block(
38 &mut self,
39 block: u32,
40 buffer: &[u8; BLOCK_SIZE],
41 ) -> Result<(), Self::Error> {
42 (**self).write_block(block, buffer).await
43 }
44}
45
46impl<D: BlockDevice + ?Sized> BlockDevice for &mut D {
48 type Error = D::Error;
49
50 async fn read_block(
51 &mut self,
52 block: u32,
53 buffer: &mut [u8; BLOCK_SIZE],
54 ) -> Result<(), Self::Error> {
55 (**self).read_block(block, buffer).await
56 }
57
58 async fn num_blocks(&mut self) -> Result<u32, Self::Error> {
59 (**self).num_blocks().await
60 }
61}