|
41 | 41 | /// with a CS (Chip Select) pin.
|
42 | 42 | ///
|
43 | 43 | /// See (the docs on embedded-hal)[embedded_hal::spi::blocking] for important information on SPI Bus vs Device traits.
|
44 |
| -pub trait SpiDevice: ErrorType { |
| 44 | +/// |
| 45 | +/// # Safety |
| 46 | +/// |
| 47 | +/// See [`SpiDevice::transaction`] for details. |
| 48 | +pub unsafe trait SpiDevice: ErrorType { |
45 | 49 | /// SPI Bus type for this device.
|
46 | 50 | type Bus: ErrorType;
|
47 | 51 |
|
@@ -69,9 +73,13 @@ pub trait SpiDevice: ErrorType {
|
69 | 73 | /// On bus errors the implementation should try to deassert CS.
|
70 | 74 | /// If an error occurs while deasserting CS the bus error should take priority as the return value.
|
71 | 75 | ///
|
| 76 | + /// # Safety |
| 77 | + /// |
72 | 78 | /// The current state of the Rust typechecker doesn't allow expressing the necessary lifetime constraints, so
|
73 |
| - /// the `f` closure receives a lifetime-less `*mut Bus` raw pointer instead. The pointer is guaranteed |
74 |
| - /// to be valid for the entire duration the closure is running, so dereferencing it is safe. |
| 79 | + /// the `f` closure receives a lifetime-less `*mut Bus` raw pointer instead. |
| 80 | + /// |
| 81 | + /// Implementers of the `SpiDevice` trait must guarantee that the pointer is valid and dereferencable |
| 82 | + /// for the entire duration of the closure. |
75 | 83 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
|
76 | 84 | where
|
77 | 85 | F: FnOnce(*mut Self::Bus) -> Fut + 'a,
|
@@ -153,7 +161,7 @@ pub trait SpiDevice: ErrorType {
|
153 | 161 | }
|
154 | 162 | }
|
155 | 163 |
|
156 |
| -impl<T: SpiDevice> SpiDevice for &mut T { |
| 164 | +unsafe impl<T: SpiDevice> SpiDevice for &mut T { |
157 | 165 | type Bus = T::Bus;
|
158 | 166 |
|
159 | 167 | type TransactionFuture<'a, R, F, Fut> = T::TransactionFuture<'a, R, F, Fut>
|
@@ -377,7 +385,7 @@ where
|
377 | 385 | }
|
378 | 386 | }
|
379 | 387 |
|
380 |
| -impl<BUS, CS> SpiDevice for ExclusiveDevice<BUS, CS> |
| 388 | +unsafe impl<BUS, CS> SpiDevice for ExclusiveDevice<BUS, CS> |
381 | 389 | where
|
382 | 390 | BUS: SpiBusFlush,
|
383 | 391 | CS: OutputPin,
|
|
0 commit comments