Skip to content

Commit e6e5c62

Browse files
committed
add support for async BlockDevice
1 parent 1b9fa5c commit e6e5c62

File tree

7 files changed

+212
-152
lines changed

7 files changed

+212
-152
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ byteorder = {version = "1", default-features = false}
1515
defmt = {version = "0.3", optional = true}
1616
embedded-hal = "1.0.0"
1717
embedded-io = "0.6.1"
18+
embedded-io-async = "0.6.1"
1819
heapless = "^0.8"
1920
log = {version = "0.4", default-features = false, optional = true}
21+
maybe-async = {version = "0.2"}
2022

2123
[dev-dependencies]
2224
chrono = "0.4"
@@ -27,6 +29,7 @@ hex-literal = "0.4.1"
2729
sha2 = "0.10"
2830

2931
[features]
30-
default = ["log"]
32+
default = ["log", "is_sync"]
3133
defmt-log = ["dep:defmt"]
3234
log = ["dep:log"]
35+
is_sync = ["maybe-async/is_sync"]

src/blockdevice.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ impl Default for Block {
7575

7676
/// A block device - a device which can read and write blocks (or
7777
/// sectors). Only supports devices which are <= 2 TiB in size.
78+
#[allow(async_fn_in_trait)]
79+
#[maybe_async::maybe_async(AFIT)]
7880
pub trait BlockDevice {
7981
/// The errors that the `BlockDevice` can return. Must be debug formattable.
8082
type Error: core::fmt::Debug;
8183
/// Read one or more blocks, starting at the given block index.
82-
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
84+
async fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
8385
/// Write one or more blocks, starting at the given block index.
84-
fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
86+
async fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
8587
/// Determine how many blocks this device can hold.
8688
fn num_blocks(&self) -> Result<BlockCount, Self::Error>;
8789
}
@@ -110,31 +112,34 @@ where
110112
}
111113

112114
/// Read a block, and return a reference to it.
113-
pub fn read(&mut self, block_idx: BlockIdx) -> Result<&Block, D::Error> {
115+
#[maybe_async::maybe_async]
116+
pub async fn read(&mut self, block_idx: BlockIdx) -> Result<&Block, D::Error> {
114117
if self.block_idx != Some(block_idx) {
115118
self.block_idx = None;
116-
self.block_device.read(&mut self.block, block_idx)?;
119+
self.block_device.read(&mut self.block, block_idx).await?;
117120
self.block_idx = Some(block_idx);
118121
}
119122
Ok(&self.block[0])
120123
}
121124

122125
/// Read a block, and return a reference to it.
123-
pub fn read_mut(&mut self, block_idx: BlockIdx) -> Result<&mut Block, D::Error> {
126+
#[maybe_async::maybe_async]
127+
pub async fn read_mut(&mut self, block_idx: BlockIdx) -> Result<&mut Block, D::Error> {
124128
if self.block_idx != Some(block_idx) {
125129
self.block_idx = None;
126-
self.block_device.read(&mut self.block, block_idx)?;
130+
self.block_device.read(&mut self.block, block_idx).await?;
127131
self.block_idx = Some(block_idx);
128132
}
129133
Ok(&mut self.block[0])
130134
}
131135

132136
/// Write back a block you read with [`Self::read_mut`] and then modified.
133-
pub fn write_back(&mut self) -> Result<(), D::Error> {
137+
#[maybe_async::maybe_async]
138+
pub async fn write_back(&mut self) -> Result<(), D::Error> {
134139
self.block_device.write(
135140
&self.block,
136141
self.block_idx.expect("write_back with no read"),
137-
)
142+
).await
138143
}
139144

140145
/// Access a blank sector

0 commit comments

Comments
 (0)