Commit 58587e8
Merge #380
380: async/spi: replace the "give back the Bus" hack with a raw pointer. r=ryankurte a=Dirbaio
The async SPI trait contains a "hack" to workaround limitations in Rust's borrow checker: #347
It turns out the hack doesn't allow many shared bus implementations, for example when using an async Mutex: The `transaction` method gets `&'a mut self`, and the closure wants `&'a mut Self::Bus`. This only works if the Bus is a direct field in `self`. In the mutex case, the Bus has to come from inside the `MutexGuard`, so it'll live for less than `'a`.
See https://gist.github.com/kalkyl/ad3075182d610e7b413b8bbe1228ab90 which fails with the following error:
```
error[E0597]: `bus` does not live long enough
--> src/shared_[spi.rs:78](http://spi.rs:78/):34
|
63 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
| -- lifetime `'a` defined here
...
78 | let (bus, f_res) = f(&mut bus).await;
| --^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `bus` is borrowed for `'a`
...
89 | }
| - `bus` dropped here while still borrowed
```
This is an alternative hack. If lifetimes don't work, simply don't use lifetimes at all!
- Downside: it needs `unsafe{}` in all callers of transaction (but these should be rare, many users will use the `SpiDeviceExt` helpers.
- Upside: it's now possible to write sound shared bus impls.
- Upside: it no longer requires the "give back the bus" hack, it's now possible again to use `?` inside the closure for error handling.
cc `@kalkyl`
Co-authored-by: Dario Nieuwenhuis <[email protected]>1 file changed
+30
-42
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 25 | + | |
| 26 | + | |
32 | 27 | | |
33 | 28 | | |
34 | 29 | | |
| |||
42 | 37 | | |
43 | 38 | | |
44 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
45 | 44 | | |
46 | 45 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 46 | + | |
| 47 | + | |
54 | 48 | | |
55 | 49 | | |
56 | 50 | | |
| |||
147 | 141 | | |
148 | 142 | | |
149 | 143 | | |
150 | | - | |
151 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
152 | 147 | | |
153 | 148 | | |
154 | 149 | | |
| |||
164 | 159 | | |
165 | 160 | | |
166 | 161 | | |
167 | | - | |
168 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
169 | 165 | | |
170 | 166 | | |
171 | 167 | | |
| |||
185 | 181 | | |
186 | 182 | | |
187 | 183 | | |
188 | | - | |
189 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
190 | 187 | | |
191 | 188 | | |
192 | 189 | | |
| |||
205 | 202 | | |
206 | 203 | | |
207 | 204 | | |
208 | | - | |
209 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
210 | 208 | | |
211 | 209 | | |
212 | 210 | | |
| |||
216 | 214 | | |
217 | 215 | | |
218 | 216 | | |
219 | | - | |
220 | | - | |
| 217 | + | |
| 218 | + | |
221 | 219 | | |
222 | 220 | | |
223 | 221 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
| 222 | + | |
| 223 | + | |
231 | 224 | | |
232 | 225 | | |
233 | 226 | | |
| |||
449 | 442 | | |
450 | 443 | | |
451 | 444 | | |
452 | | - | |
453 | | - | |
| 445 | + | |
| 446 | + | |
454 | 447 | | |
455 | 448 | | |
456 | 449 | | |
457 | 450 | | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
| 451 | + | |
| 452 | + | |
465 | 453 | | |
466 | 454 | | |
467 | 455 | | |
468 | 456 | | |
469 | | - | |
| 457 | + | |
470 | 458 | | |
471 | 459 | | |
472 | | - | |
| 460 | + | |
473 | 461 | | |
474 | 462 | | |
475 | 463 | | |
| |||
0 commit comments