Skip to content

Commit a49432e

Browse files
committed
add support for async BlockDevice
github workflow build is_sync fix workflow got renamed run `cargo fmt`
1 parent 1b9fa5c commit a49432e

File tree

8 files changed

+350
-200
lines changed

8 files changed

+350
-200
lines changed

.github/workflows/rust.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v1
2020
- name: Build
21-
run: cargo build --no-default-features --features ${{matrix.features}} --verbose
21+
run: cargo build --no-default-features --features ${{matrix.features}},is_sync --verbose
2222
- name: Run Tests
23-
run: cargo test --no-default-features --features ${{matrix.features}} --verbose
23+
run: cargo test --no-default-features --features ${{matrix.features}},is_sync --verbose

Cargo.toml

+4-1
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

+22-11
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ 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(
85+
&self,
86+
blocks: &mut [Block],
87+
start_block_idx: BlockIdx,
88+
) -> Result<(), Self::Error>;
8389
/// Write one or more blocks, starting at the given block index.
84-
fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
90+
async fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
8591
/// Determine how many blocks this device can hold.
8692
fn num_blocks(&self) -> Result<BlockCount, Self::Error>;
8793
}
@@ -110,31 +116,36 @@ where
110116
}
111117

112118
/// Read a block, and return a reference to it.
113-
pub fn read(&mut self, block_idx: BlockIdx) -> Result<&Block, D::Error> {
119+
#[maybe_async::maybe_async]
120+
pub async fn read(&mut self, block_idx: BlockIdx) -> Result<&Block, D::Error> {
114121
if self.block_idx != Some(block_idx) {
115122
self.block_idx = None;
116-
self.block_device.read(&mut self.block, block_idx)?;
123+
self.block_device.read(&mut self.block, block_idx).await?;
117124
self.block_idx = Some(block_idx);
118125
}
119126
Ok(&self.block[0])
120127
}
121128

122129
/// Read a block, and return a reference to it.
123-
pub fn read_mut(&mut self, block_idx: BlockIdx) -> Result<&mut Block, D::Error> {
130+
#[maybe_async::maybe_async]
131+
pub async fn read_mut(&mut self, block_idx: BlockIdx) -> Result<&mut Block, D::Error> {
124132
if self.block_idx != Some(block_idx) {
125133
self.block_idx = None;
126-
self.block_device.read(&mut self.block, block_idx)?;
134+
self.block_device.read(&mut self.block, block_idx).await?;
127135
self.block_idx = Some(block_idx);
128136
}
129137
Ok(&mut self.block[0])
130138
}
131139

132140
/// Write back a block you read with [`Self::read_mut`] and then modified.
133-
pub fn write_back(&mut self) -> Result<(), D::Error> {
134-
self.block_device.write(
135-
&self.block,
136-
self.block_idx.expect("write_back with no read"),
137-
)
141+
#[maybe_async::maybe_async]
142+
pub async fn write_back(&mut self) -> Result<(), D::Error> {
143+
self.block_device
144+
.write(
145+
&self.block,
146+
self.block_idx.expect("write_back with no read"),
147+
)
148+
.await
138149
}
139150

140151
/// Access a blank sector

0 commit comments

Comments
 (0)