Skip to content

Commit

Permalink
embassy: Change polonius workaround to make the future Send
Browse files Browse the repository at this point in the history
Previously the *mut was held across .await points, so the future wasn't
Send which meant it only worked with single-threaded executors.
  • Loading branch information
mkj committed Jun 16, 2024
1 parent 3d6a72c commit 46a3a9a
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions embassy/src/embassy_sunset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,12 @@ impl<'a> EmbassySunset<'a> {
pub(crate) async fn progress<'g, 'f>(&'g self, ph: &'f mut ProgressHolder<'g, 'a>)
-> Result<Event<'f, 'a>>
{
ph.g = None;
#[cfg(feature = "try-polonius")]
let guard = &mut ph.g;
*guard = None;

#[cfg(not(feature = "try-polonius"))]
let guardptr = guard as *mut Option<_>;

// poll progress until we get an actual event to return
loop {
debug_assert!(guard.is_none());

// Safety: At the start of the loop iteration nothing is borrowing from
// guard, it is set to None. We dereference through a pointer to lose the 'f
Expand All @@ -313,7 +310,8 @@ impl<'a> EmbassySunset<'a> {
// Once polonius is implemented this is unnecessary. polonius-the-crab
// can't be used since it would require an async closure.
#[cfg(not(feature = "try-polonius"))]
let guard = unsafe { &mut *guardptr };
let guard = unsafe { &mut *(&mut ph.g as *mut Option<_>) };
debug_assert!(guard.is_none());

let idle = {
let inner = guard.insert(self.inner.lock().await);
Expand All @@ -336,7 +334,7 @@ impl<'a> EmbassySunset<'a> {
// Safety: No borrows of guard remain, can lose the inferred 'f lifetime.
// Not required after polonius.
#[cfg(not(feature = "try-polonius"))]
let guard = unsafe { &mut *guardptr };
let guard = unsafe { &mut *(&mut ph.g as *mut Option<_>) };

// Drop the Mutex
*guard = None;
Expand Down

0 comments on commit 46a3a9a

Please sign in to comment.