Closed Dirbaio closed 2 years ago
r? @ryankurte
(rust-highfive has picked a reviewer for you, use r? to override)
hmmm interesting, so mutexes in embassy are a singleton with shared references rather than a cheaply cloned object?
ime in the async world you'd usually clone the mutex before the async move
to avoid the issue, just having a play to see if there are any other strange workarounds.
(one thought is to return an actual transaction object implementing Future
with poll over a set of states / containing the refs, but i can't tell if this helps yet)
mutexes in embassy are a singleton with shared references rather than a cheaply cloned object?
It works the same as std or tokio's Mutex. Maybe you're confusing it with Arc<Mutex<..>>
?
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 inself
. In the mutex case, the Bus has to come from inside theMutexGuard
, so it'll live for less than'a
.See https://gist.github.com/kalkyl/ad3075182d610e7b413b8bbe1228ab90 which fails with the following error:
This is an alternative hack. If lifetimes don't work, simply don't use lifetimes at all!
unsafe{}
in all callers of transaction (but these should be rare, many users will use theSpiDeviceExt
helpers.?
inside the closure for error handling.cc @kalkyl