Skip to content

Commit d92d391

Browse files
authoredJul 6, 2023
feat(rt): add downcast on Sleep trait (#3125)
This commit allows downcasting pinned `Sleep` object to support implementations of `Timer::reset`. One example where this is useful is when using `TokioSleep`, i.e. `Sleep` provided by tokio. Closes #3027
1 parent d977f20 commit d92d391

File tree

3 files changed

+141
-25
lines changed

3 files changed

+141
-25
lines changed
 

‎benches/support/tokiort.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ impl Timer for TokioTimer {
4141
inner: tokio::time::sleep_until(deadline.into()),
4242
})
4343
}
44+
45+
fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
46+
if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() {
47+
sleep.reset(new_deadline.into())
48+
}
49+
}
4450
}
4551

4652
struct TokioTimeout<T> {
@@ -75,7 +81,10 @@ impl Future for TokioSleep {
7581
}
7682
}
7783

78-
// Use HasSleep to get tokio::time::Sleep to implement Unpin.
79-
// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html
80-
8184
impl Sleep for TokioSleep {}
85+
86+
impl TokioSleep {
87+
pub fn reset(self: Pin<&mut Self>, deadline: Instant) {
88+
self.project().inner.as_mut().reset(deadline.into());
89+
}
90+
}

‎src/rt/mod.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
//! to plug in other runtimes.
77
88
pub mod bounds;
9+
mod timer;
910

10-
use std::{
11-
future::Future,
12-
pin::Pin,
13-
time::{Duration, Instant},
14-
};
11+
pub use timer::{Sleep, Timer};
1512

1613
/// An executor of futures.
1714
///
@@ -39,20 +36,3 @@ pub trait Executor<Fut> {
3936
/// Place the future into the executor to be run.
4037
fn execute(&self, fut: Fut);
4138
}
42-
43-
/// A timer which provides timer-like functions.
44-
pub trait Timer {
45-
/// Return a future that resolves in `duration` time.
46-
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>>;
47-
48-
/// Return a future that resolves at `deadline`.
49-
fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>>;
50-
51-
/// Reset a future to resolve at `new_deadline` instead.
52-
fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
53-
*sleep = self.sleep_until(new_deadline);
54-
}
55-
}
56-
57-
/// A future returned by a `Timer`.
58-
pub trait Sleep: Send + Sync + Future<Output = ()> {}

‎src/rt/timer.rs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//! Provides a timer trait with timer-like functions
2+
//!
3+
//! Example using tokio timer:
4+
//! ```rust
5+
//! use std::{
6+
//! pin::Pin,
7+
//! task::{Context, Poll},
8+
//! time::{Duration, Instant},
9+
//! };
10+
//!
11+
//! use futures_util::Future;
12+
//! use pin_project_lite::pin_project;
13+
//! use hyper::rt::{Timer, Sleep};
14+
//!
15+
//! #[derive(Clone, Debug)]
16+
//! pub struct TokioTimer;
17+
//!
18+
//! impl Timer for TokioTimer {
19+
//! fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
20+
//! Box::pin(TokioSleep {
21+
//! inner: tokio::time::sleep(duration),
22+
//! })
23+
//! }
24+
//!
25+
//! fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> {
26+
//! Box::pin(TokioSleep {
27+
//! inner: tokio::time::sleep_until(deadline.into()),
28+
//! })
29+
//! }
30+
//!
31+
//! fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
32+
//! if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() {
33+
//! sleep.reset(new_deadline.into())
34+
//! }
35+
//! }
36+
//! }
37+
//!
38+
//! pin_project! {
39+
//! pub(crate) struct TokioSleep {
40+
//! #[pin]
41+
//! pub(crate) inner: tokio::time::Sleep,
42+
//! }
43+
//! }
44+
//!
45+
//! impl Future for TokioSleep {
46+
//! type Output = ();
47+
//!
48+
//! fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
49+
//! self.project().inner.poll(cx)
50+
//! }
51+
//! }
52+
//!
53+
//! impl Sleep for TokioSleep {}
54+
//!
55+
//! impl TokioSleep {
56+
//! pub fn reset(self: Pin<&mut Self>, deadline: Instant) {
57+
//! self.project().inner.as_mut().reset(deadline.into());
58+
//! }
59+
//! }
60+
//! ````
61+
62+
use std::{
63+
any::TypeId,
64+
future::Future,
65+
pin::Pin,
66+
time::{Duration, Instant},
67+
};
68+
69+
/// A timer which provides timer-like functions.
70+
pub trait Timer {
71+
/// Return a future that resolves in `duration` time.
72+
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>>;
73+
74+
/// Return a future that resolves at `deadline`.
75+
fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>>;
76+
77+
/// Reset a future to resolve at `new_deadline` instead.
78+
fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
79+
*sleep = self.sleep_until(new_deadline);
80+
}
81+
}
82+
83+
/// A future returned by a `Timer`.
84+
pub trait Sleep: Send + Sync + Future<Output = ()> {
85+
#[doc(hidden)]
86+
/// This method is private and can not be implemented by downstream crate
87+
fn __type_id(&self, _: private::Sealed) -> TypeId
88+
where
89+
Self: 'static,
90+
{
91+
TypeId::of::<Self>()
92+
}
93+
}
94+
95+
impl dyn Sleep {
96+
//! This is a re-implementation of downcast methods from std::any::Any
97+
98+
/// Check whether the type is the same as `T`
99+
pub fn is<T>(&self) -> bool
100+
where
101+
T: Sleep + 'static,
102+
{
103+
self.__type_id(private::Sealed {}) == TypeId::of::<T>()
104+
}
105+
106+
/// Downcast a pinned &mut Sleep object to its original type
107+
pub fn downcast_mut_pin<T>(self: Pin<&mut Self>) -> Option<Pin<&'static mut T>>
108+
where
109+
T: Sleep + 'static,
110+
{
111+
if self.is::<T>() {
112+
unsafe {
113+
let inner = Pin::into_inner_unchecked(self);
114+
Some(Pin::new_unchecked(
115+
&mut *(&mut *inner as *mut dyn Sleep as *mut T),
116+
))
117+
}
118+
} else {
119+
None
120+
}
121+
}
122+
}
123+
124+
mod private {
125+
#![allow(missing_debug_implementations)]
126+
pub struct Sealed {}
127+
}

0 commit comments

Comments
 (0)
Please sign in to comment.