From 90046cc641ac682af51d83b27e6a5c1b739715ee Mon Sep 17 00:00:00 2001 From: jtnunley Date: Tue, 17 Jan 2023 13:01:51 -0800 Subject: [PATCH 1/2] Improve drop ergonomics of Ready --- src/reactor.rs | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/reactor.rs b/src/reactor.rs index 32a2a11..ba2aaef 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -476,7 +476,7 @@ impl Source { dir, ticks: None, index: None, - _guard: None, + _capture: PhantomData, } } } @@ -566,7 +566,7 @@ struct Ready>, T> { dir: usize, ticks: Option<(usize, usize)>, index: Option, - _guard: Option>, + _capture: PhantomData>, } impl>, T> Unpin for Ready {} @@ -580,7 +580,6 @@ impl> + Clone, T> Future for Ready { dir, ticks, index, - _guard, .. } = &mut *self; @@ -602,12 +601,6 @@ impl> + Clone, T> Future for Ready { Some(i) => i, None => { let i = state[*dir].wakers.insert(None); - *_guard = Some(RemoveOnDrop { - handle: handle.clone(), - dir: *dir, - key: i, - _marker: PhantomData, - }); *index = Some(i); *ticks = Some((Reactor::get().ticker(), state[*dir].tick)); i @@ -631,20 +624,15 @@ impl> + Clone, T> Future for Ready { } } -/// Remove waker when dropped. -struct RemoveOnDrop>, T> { - handle: H, - dir: usize, - key: usize, - _marker: PhantomData T>, -} - -impl>, T> Drop for RemoveOnDrop { +impl>, T> Drop for Ready { fn drop(&mut self) { - let mut state = self.handle.borrow().source.state.lock().unwrap(); - let wakers = &mut state[self.dir].wakers; - if wakers.contains(self.key) { - wakers.remove(self.key); + // Remove our waker when dropped. + if let Some(key) = self.index { + let mut state = self.handle.borrow().source.state.lock().unwrap(); + let wakers = &mut state[self.dir].wakers; + if wakers.contains(key) { + wakers.remove(key); + } } } } From a1634d3a07029d6f2148c00ed81a0269779cdf88 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Tue, 17 Jan 2023 13:03:04 -0800 Subject: [PATCH 2/2] Mixed up PhantomData --- src/reactor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reactor.rs b/src/reactor.rs index ba2aaef..6b933ba 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -566,7 +566,7 @@ struct Ready>, T> { dir: usize, ticks: Option<(usize, usize)>, index: Option, - _capture: PhantomData>, + _capture: PhantomData T>, } impl>, T> Unpin for Ready {}