diff --git a/benches/support/tokiort.rs b/benches/support/tokiort.rs index bfcbfcd062..4708bd67a1 100644 --- a/benches/support/tokiort.rs +++ b/benches/support/tokiort.rs @@ -43,8 +43,8 @@ impl Timer for TokioTimer { } fn reset(&self, sleep: &mut Pin>, new_deadline: Instant) { - if sleep.downcast_ref::().is_some() { - *sleep = self.sleep_until(new_deadline); + if let Some(sleep) = sleep.as_mut().downcast_mut_pin::() { + sleep.reset(new_deadline.into()) } } } @@ -81,7 +81,10 @@ impl Future for TokioSleep { } } -// Use HasSleep to get tokio::time::Sleep to implement Unpin. -// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html - impl Sleep for TokioSleep {} + +impl TokioSleep { + pub fn reset(self: Pin<&mut Self>, deadline: Instant) { + self.project().inner.as_mut().reset(deadline.into()); + } +} diff --git a/src/rt/timer.rs b/src/rt/timer.rs index 2b59bc7608..b482b062c3 100644 --- a/src/rt/timer.rs +++ b/src/rt/timer.rs @@ -29,8 +29,8 @@ //! } //! //! fn reset(&self, sleep: &mut Pin>, new_deadline: Instant) { -//! if sleep.downcast_ref::().is_some() { -//! *sleep = self.sleep_until(new_deadline); +//! if let Some(sleep) = sleep.as_mut().downcast_mut_pin::() { +//! sleep.reset(new_deadline.into()) //! } //! } //! } @@ -51,6 +51,12 @@ //! } //! //! impl Sleep for TokioSleep {} +//! +//! impl TokioSleep { +//! pub fn reset(self: Pin<&mut Self>, deadline: Instant) { +//! self.project().inner.as_mut().reset(deadline.into()); +//! } +//! } //! ```` use std::{ @@ -120,6 +126,23 @@ impl dyn Sleep { None } } + + /// Downcast a pinned &mut Sleep object to its original type + pub fn downcast_mut_pin(self: Pin<&mut Self>) -> Option> + where + T: Sleep + 'static, + { + if self.is::() { + unsafe { + let inner = Pin::into_inner_unchecked(self); + Some(Pin::new_unchecked( + &mut *(&mut *inner as *mut dyn Sleep as *mut T), + )) + } + } else { + None + } + } } mod private {