Skip to content

Commit 1e2fb04

Browse files
committed
Switch to async-fn-in-trait
1 parent 758f5d7 commit 1e2fb04

File tree

47 files changed

+1098
-1524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1098
-1524
lines changed

embassy-embedded-hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ nightly = ["embedded-hal-async", "embedded-storage-async"]
2020
embassy-sync = { version = "0.1.0", path = "../embassy-sync" }
2121
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
2222
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
23-
embedded-hal-async = { version = "=0.1.0-alpha.3", optional = true }
23+
embedded-hal-async = { version = "=0.2.0-alpha.0", optional = true }
2424
embedded-storage = "0.3.0"
2525
embedded-storage-async = { version = "0.3.0", optional = true }
2626
nb = "1.0.0"

embassy-embedded-hal/src/adapter.rs

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,31 @@ where
3838
E: embedded_hal_1::i2c::Error + 'static,
3939
T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>,
4040
{
41-
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
42-
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
43-
type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
44-
45-
fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
46-
async move { self.wrapped.read(address, buffer) }
41+
async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), Self::Error> {
42+
self.wrapped.read(address, buffer)
4743
}
4844

49-
fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
50-
async move { self.wrapped.write(address, bytes) }
45+
async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), Self::Error> {
46+
self.wrapped.write(address, bytes)
5147
}
5248

53-
fn write_read<'a>(&'a mut self, address: u8, bytes: &'a [u8], buffer: &'a mut [u8]) -> Self::WriteReadFuture<'a> {
54-
async move { self.wrapped.write_read(address, bytes, buffer) }
49+
async fn write_read<'a>(
50+
&'a mut self,
51+
address: u8,
52+
bytes: &'a [u8],
53+
buffer: &'a mut [u8],
54+
) -> Result<(), Self::Error> {
55+
self.wrapped.write_read(address, bytes, buffer)
5556
}
5657

57-
type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
58-
59-
fn transaction<'a, 'b>(
58+
async fn transaction<'a, 'b>(
6059
&'a mut self,
6160
address: u8,
6261
operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
63-
) -> Self::TransactionFuture<'a, 'b> {
62+
) -> Result<(), Self::Error> {
6463
let _ = address;
6564
let _ = operations;
66-
async move { todo!() }
65+
todo!()
6766
}
6867
}
6968

@@ -84,23 +83,17 @@ where
8483
E: embedded_hal_1::spi::Error + 'static,
8584
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
8685
{
87-
type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
88-
89-
fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> {
90-
async move {
91-
// Ensure we write the expected bytes
92-
for i in 0..core::cmp::min(read.len(), write.len()) {
93-
read[i] = write[i].clone();
94-
}
95-
self.wrapped.transfer(read)?;
96-
Ok(())
86+
async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> {
87+
// Ensure we write the expected bytes
88+
for i in 0..core::cmp::min(read.len(), write.len()) {
89+
read[i] = write[i].clone();
9790
}
91+
self.wrapped.transfer(read)?;
92+
Ok(())
9893
}
9994

100-
type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
101-
102-
fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> {
103-
async move { todo!() }
95+
async fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Result<(), Self::Error> {
96+
todo!()
10497
}
10598
}
10699

@@ -109,10 +102,8 @@ where
109102
E: embedded_hal_1::spi::Error + 'static,
110103
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
111104
{
112-
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
113-
114-
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
115-
async move { Ok(()) }
105+
async fn flush(&mut self) -> Result<(), Self::Error> {
106+
Ok(())
116107
}
117108
}
118109

@@ -121,13 +112,9 @@ where
121112
E: embedded_hal_1::spi::Error + 'static,
122113
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
123114
{
124-
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
125-
126-
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
127-
async move {
128-
self.wrapped.write(data)?;
129-
Ok(())
130-
}
115+
async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
116+
self.wrapped.write(data)?;
117+
Ok(())
131118
}
132119
}
133120

@@ -136,13 +123,9 @@ where
136123
E: embedded_hal_1::spi::Error + 'static,
137124
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
138125
{
139-
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
140-
141-
fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
142-
async move {
143-
self.wrapped.transfer(data)?;
144-
Ok(())
145-
}
126+
async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
127+
self.wrapped.transfer(data)?;
128+
Ok(())
146129
}
147130
}
148131

@@ -192,7 +175,7 @@ where
192175
}
193176

194177
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a;
195-
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
178+
fn flush(&mut self) -> Result<(), Self::Error> {
196179
async move { self.wrapped.bflush() }
197180
}
198181
}

embassy-embedded-hal/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2-
#![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))]
2+
#![cfg_attr(
3+
feature = "nightly",
4+
feature(type_alias_impl_trait, async_fn_in_trait, impl_trait_projections)
5+
)]
6+
#![cfg_attr(feature = "nightly", allow(incomplete_features))]
37
#![warn(missing_docs)]
48

59
//! Utilities to use `embedded-hal` traits with Embassy.

embassy-embedded-hal/src/shared_bus/asynch/i2c.rs

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
//! let i2c_dev2 = I2cDevice::new(i2c_bus);
2323
//! let mpu = Mpu6050::new(i2c_dev2);
2424
//! ```
25-
use core::future::Future;
2625
2726
use embassy_sync::blocking_mutex::raw::RawMutex;
2827
use embassy_sync::mutex::Mutex;
@@ -55,53 +54,39 @@ where
5554
M: RawMutex + 'static,
5655
BUS: i2c::I2c + 'static,
5756
{
58-
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
59-
60-
fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
61-
async move {
62-
let mut bus = self.bus.lock().await;
63-
bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
64-
Ok(())
65-
}
57+
async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
58+
let mut bus = self.bus.lock().await;
59+
bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
60+
Ok(())
6661
}
6762

68-
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
69-
70-
fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
71-
async move {
72-
let mut bus = self.bus.lock().await;
73-
bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
74-
Ok(())
75-
}
63+
async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
64+
let mut bus = self.bus.lock().await;
65+
bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
66+
Ok(())
7667
}
7768

78-
type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
79-
80-
fn write_read<'a>(
69+
async fn write_read<'a>(
8170
&'a mut self,
8271
address: u8,
8372
wr_buffer: &'a [u8],
8473
rd_buffer: &'a mut [u8],
85-
) -> Self::WriteReadFuture<'a> {
86-
async move {
87-
let mut bus = self.bus.lock().await;
88-
bus.write_read(address, wr_buffer, rd_buffer)
89-
.await
90-
.map_err(I2cDeviceError::I2c)?;
91-
Ok(())
92-
}
74+
) -> Result<(), I2cDeviceError<BUS::Error>> {
75+
let mut bus = self.bus.lock().await;
76+
bus.write_read(address, wr_buffer, rd_buffer)
77+
.await
78+
.map_err(I2cDeviceError::I2c)?;
79+
Ok(())
9380
}
9481

95-
type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
96-
97-
fn transaction<'a, 'b>(
82+
async fn transaction<'a, 'b>(
9883
&'a mut self,
9984
address: u8,
10085
operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
101-
) -> Self::TransactionFuture<'a, 'b> {
86+
) -> Result<(), I2cDeviceError<BUS::Error>> {
10287
let _ = address;
10388
let _ = operations;
104-
async move { todo!() }
89+
todo!()
10590
}
10691
}
10792

@@ -136,55 +121,41 @@ where
136121
M: RawMutex + 'static,
137122
BUS: i2c::I2c + SetConfig + 'static,
138123
{
139-
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
140-
141-
fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
142-
async move {
143-
let mut bus = self.bus.lock().await;
144-
bus.set_config(&self.config);
145-
bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
146-
Ok(())
147-
}
124+
async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
125+
let mut bus = self.bus.lock().await;
126+
bus.set_config(&self.config);
127+
bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?;
128+
Ok(())
148129
}
149130

150-
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
151-
152-
fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
153-
async move {
154-
let mut bus = self.bus.lock().await;
155-
bus.set_config(&self.config);
156-
bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
157-
Ok(())
158-
}
131+
async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), I2cDeviceError<BUS::Error>> {
132+
let mut bus = self.bus.lock().await;
133+
bus.set_config(&self.config);
134+
bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?;
135+
Ok(())
159136
}
160137

161-
type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
162-
163-
fn write_read<'a>(
138+
async fn write_read<'a>(
164139
&'a mut self,
165140
address: u8,
166141
wr_buffer: &'a [u8],
167142
rd_buffer: &'a mut [u8],
168-
) -> Self::WriteReadFuture<'a> {
169-
async move {
170-
let mut bus = self.bus.lock().await;
171-
bus.set_config(&self.config);
172-
bus.write_read(address, wr_buffer, rd_buffer)
173-
.await
174-
.map_err(I2cDeviceError::I2c)?;
175-
Ok(())
176-
}
143+
) -> Result<(), I2cDeviceError<BUS::Error>> {
144+
let mut bus = self.bus.lock().await;
145+
bus.set_config(&self.config);
146+
bus.write_read(address, wr_buffer, rd_buffer)
147+
.await
148+
.map_err(I2cDeviceError::I2c)?;
149+
Ok(())
177150
}
178151

179-
type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
180-
181-
fn transaction<'a, 'b>(
152+
async fn transaction<'a, 'b>(
182153
&'a mut self,
183154
address: u8,
184155
operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
185-
) -> Self::TransactionFuture<'a, 'b> {
156+
) -> Result<(), I2cDeviceError<BUS::Error>> {
186157
let _ = address;
187158
let _ = operations;
188-
async move { todo!() }
159+
todo!()
189160
}
190161
}

0 commit comments

Comments
 (0)