Code cleanup in shared.rs
Replaced several uses of `if let` with a more idiomatic `match`
added `will_wake` optimization
cleaned up verbose logic in `record_waker`
rustfmt
-use crate::task::{ArcWake, waker_ref};
+use crate::task::{waker_ref, ArcWake};
use futures_core::future::{FusedFuture, Future};
use futures_core::task::{Context, Poll, Waker};
use slab::Slab;
@@ -54,13 +54,15 @@ unsafe impl<Fut> Send for Inner<Fut>
where
Fut: Future + Send,
Fut::Output: Send + Sync,
-{}
+{
+}
unsafe impl<Fut> Sync for Inner<Fut>
where
Fut: Future + Send,
Fut::Output: Send + Sync,
-{}
+{
+}
const IDLE: usize = 0;
const POLLING: usize = 1;
@@ -125,27 +127,20 @@ where
fn record_waker(&self, waker_key: &mut usize, cx: &mut Context<'_>) {
let mut wakers_guard = self.notifier.wakers.lock().unwrap();
- let wakers = if let Some(wakers) = wakers_guard.as_mut() {
- wakers
- } else {
- return;
+ let wakers = match wakers_guard.as_mut() {
+ Some(wakers) => wakers,
+ None => return,
};
+ let new_waker = cx.waker();
+
if *waker_key == NULL_WAKER_KEY {
- *waker_key = wakers.insert(Some(cx.waker().clone()));
+ *waker_key = wakers.insert(Some(new_waker.clone()));
} else {
- let waker_slot = &mut wakers[*waker_key];
- let needs_replacement = if let Some(_old_waker) = waker_slot {
- // If there's still an unwoken waker in the slot, only replace
- // if the current one wouldn't wake the same task.
- // TODO: This API is currently not available, so replace always
- // !waker.will_wake_nonlocal(old_waker)
- true
- } else {
- true
- };
- if needs_replacement {
- *waker_slot = Some(cx.waker().clone());
+ match wakers[*waker_key] {
+ Some(ref old_waker) if new_waker.will_wake(old_waker) => {}
+ // Could use clone_from here, but Waker doesn't specialize it.
+ ref mut slot => *slot = Some(new_waker.clone()),
}
}
debug_assert!(*waker_key != NULL_WAKER_KEY);
@@ -184,7 +179,10 @@ where
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
- let inner = this.inner.take().expect("Shared future polled again after completion");
+ let inner = this
+ .inner
+ .take()
+ .expect("Shared future polled again after completion");
// Fast path for when the wrapped future has already completed
if inner.notifier.state.load(Acquire) == COMPLETE {
@@ -262,8 +260,7 @@ where
};
unsafe {
- *inner.future_or_output.get() =
- FutureOrOutput::Output(output);
+ *inner.future_or_output.get() = FutureOrOutput::Output(output);
}
inner.notifier.state.store(COMPLETE, SeqCst);
