Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the async SpiDevice trait unsafe to implement #403

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions embedded-hal-async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- spi: device helper methods (`read`, `write`, `transfer`...) are now default methods in `SpiDevice` instead of an `SpiDeviceExt` extension trait.
- spi: the `SpiDevice::transaction` closure now gets a raw pointer to the `SpiBus` to work around Rust borrow checker limitations.
- spi: the `SpiDevice` trait is now unsafe to implement due to the raw pointer workaround.


## [v0.1.0-alpha.0] - 2022-04-17
Expand Down
18 changes: 13 additions & 5 deletions embedded-hal-async/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ where
/// with a CS (Chip Select) pin.
///
/// See (the docs on embedded-hal)[embedded_hal::spi::blocking] for important information on SPI Bus vs Device traits.
pub trait SpiDevice: ErrorType {
///
/// # Safety
///
/// See [`SpiDevice::transaction`] for details.
pub unsafe trait SpiDevice: ErrorType {
/// SPI Bus type for this device.
type Bus: ErrorType;

Expand Down Expand Up @@ -69,9 +73,13 @@ pub trait SpiDevice: ErrorType {
/// On bus errors the implementation should try to deassert CS.
/// If an error occurs while deasserting CS the bus error should take priority as the return value.
///
/// # Safety
///
/// The current state of the Rust typechecker doesn't allow expressing the necessary lifetime constraints, so
/// the `f` closure receives a lifetime-less `*mut Bus` raw pointer instead. The pointer is guaranteed
/// to be valid for the entire duration the closure is running, so dereferencing it is safe.
/// the `f` closure receives a lifetime-less `*mut Bus` raw pointer instead.
///
/// Implementers of the `SpiDevice` trait must guarantee that the pointer is valid and dereferencable
/// for the entire duration of the closure.
fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
where
F: FnOnce(*mut Self::Bus) -> Fut + 'a,
Expand Down Expand Up @@ -153,7 +161,7 @@ pub trait SpiDevice: ErrorType {
}
}

impl<T: SpiDevice> SpiDevice for &mut T {
unsafe impl<T: SpiDevice> SpiDevice for &mut T {
type Bus = T::Bus;

type TransactionFuture<'a, R, F, Fut> = T::TransactionFuture<'a, R, F, Fut>
Expand Down Expand Up @@ -377,7 +385,7 @@ where
}
}

impl<BUS, CS> SpiDevice for ExclusiveDevice<BUS, CS>
unsafe impl<BUS, CS> SpiDevice for ExclusiveDevice<BUS, CS>
where
BUS: SpiBusFlush,
CS: OutputPin,
Expand Down