Skip to content

Commit 5fffa8f

Browse files
authored
m: Improve drop ergonomics of Ready (#109)
1 parent 9b46fc0 commit 5fffa8f

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

src/reactor.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl Source {
476476
dir,
477477
ticks: None,
478478
index: None,
479-
_guard: None,
479+
_capture: PhantomData,
480480
}
481481
}
482482
}
@@ -566,7 +566,7 @@ struct Ready<H: Borrow<crate::Async<T>>, T> {
566566
dir: usize,
567567
ticks: Option<(usize, usize)>,
568568
index: Option<usize>,
569-
_guard: Option<RemoveOnDrop<H, T>>,
569+
_capture: PhantomData<fn() -> T>,
570570
}
571571

572572
impl<H: Borrow<crate::Async<T>>, T> Unpin for Ready<H, T> {}
@@ -580,7 +580,6 @@ impl<H: Borrow<crate::Async<T>> + Clone, T> Future for Ready<H, T> {
580580
dir,
581581
ticks,
582582
index,
583-
_guard,
584583
..
585584
} = &mut *self;
586585

@@ -602,12 +601,6 @@ impl<H: Borrow<crate::Async<T>> + Clone, T> Future for Ready<H, T> {
602601
Some(i) => i,
603602
None => {
604603
let i = state[*dir].wakers.insert(None);
605-
*_guard = Some(RemoveOnDrop {
606-
handle: handle.clone(),
607-
dir: *dir,
608-
key: i,
609-
_marker: PhantomData,
610-
});
611604
*index = Some(i);
612605
*ticks = Some((Reactor::get().ticker(), state[*dir].tick));
613606
i
@@ -631,20 +624,15 @@ impl<H: Borrow<crate::Async<T>> + Clone, T> Future for Ready<H, T> {
631624
}
632625
}
633626

634-
/// Remove waker when dropped.
635-
struct RemoveOnDrop<H: Borrow<crate::Async<T>>, T> {
636-
handle: H,
637-
dir: usize,
638-
key: usize,
639-
_marker: PhantomData<fn() -> T>,
640-
}
641-
642-
impl<H: Borrow<crate::Async<T>>, T> Drop for RemoveOnDrop<H, T> {
627+
impl<H: Borrow<crate::Async<T>>, T> Drop for Ready<H, T> {
643628
fn drop(&mut self) {
644-
let mut state = self.handle.borrow().source.state.lock().unwrap();
645-
let wakers = &mut state[self.dir].wakers;
646-
if wakers.contains(self.key) {
647-
wakers.remove(self.key);
629+
// Remove our waker when dropped.
630+
if let Some(key) = self.index {
631+
let mut state = self.handle.borrow().source.state.lock().unwrap();
632+
let wakers = &mut state[self.dir].wakers;
633+
if wakers.contains(key) {
634+
wakers.remove(key);
635+
}
648636
}
649637
}
650638
}

0 commit comments

Comments
 (0)