Skip to content

Commit 8790fee

Browse files
authoredDec 13, 2022
feat(rt): Clean up Timer trait (#3037)
Closes #3028 BREAKING CHANGE: The return types of `Timer` have been changed.
1 parent 54eaf7f commit 8790fee

File tree

6 files changed

+24
-28
lines changed

6 files changed

+24
-28
lines changed
 

‎benches/support/tokiort.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88

99
use futures_util::Future;
1010
use hyper::rt::{Sleep, Timer};
11+
use pin_project_lite::pin_project;
1112

1213
#[derive(Clone)]
1314
/// An Executor that uses the tokio runtime.
@@ -29,16 +30,16 @@ where
2930
pub struct TokioTimer;
3031

3132
impl Timer for TokioTimer {
32-
fn sleep(&self, duration: Duration) -> Box<dyn Sleep + Unpin> {
33-
let s = tokio::time::sleep(duration);
34-
let hs = TokioSleep { inner: Box::pin(s) };
35-
return Box::new(hs);
33+
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
34+
Box::pin(TokioSleep {
35+
inner: tokio::time::sleep(duration),
36+
})
3637
}
3738

38-
fn sleep_until(&self, deadline: Instant) -> Box<dyn Sleep + Unpin> {
39-
return Box::new(TokioSleep {
40-
inner: Box::pin(tokio::time::sleep_until(deadline.into())),
41-
});
39+
fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> {
40+
Box::pin(TokioSleep {
41+
inner: tokio::time::sleep_until(deadline.into()),
42+
})
4243
}
4344
}
4445

@@ -59,15 +60,18 @@ where
5960

6061
// Use TokioSleep to get tokio::time::Sleep to implement Unpin.
6162
// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html
62-
pub(crate) struct TokioSleep {
63-
pub(crate) inner: Pin<Box<tokio::time::Sleep>>,
63+
pin_project! {
64+
pub(crate) struct TokioSleep {
65+
#[pin]
66+
pub(crate) inner: tokio::time::Sleep,
67+
}
6468
}
6569

6670
impl Future for TokioSleep {
6771
type Output = ();
6872

69-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
70-
self.inner.as_mut().poll(cx)
73+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
74+
self.project().inner.poll(cx)
7175
}
7276
}
7377

‎src/common/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,3 @@ cfg_proto! {
2828
pub(crate) use std::marker::Unpin;
2929
}
3030
pub(crate) use std::{future::Future, pin::Pin};
31-
32-
pub(crate) fn into_pin<T: ?Sized>(boxed: Box<T>) -> Pin<Box<T>> {
33-
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
34-
// when `T: !Unpin`, so it's safe to pin it directly without any
35-
// additional requirements.
36-
unsafe { Pin::new_unchecked(boxed) }
37-
}

‎src/common/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<F> Future for HyperTimeout<F> where F: Future {
5555
*/
5656

5757
impl Time {
58-
pub(crate) fn sleep(&self, duration: Duration) -> Box<dyn Sleep + Unpin> {
58+
pub(crate) fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
5959
match *self {
6060
Time::Empty => {
6161
panic!("You must supply a timer.")
@@ -64,7 +64,7 @@ impl Time {
6464
}
6565
}
6666

67-
pub(crate) fn sleep_until(&self, deadline: Instant) -> Box<dyn Sleep + Unpin> {
67+
pub(crate) fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> {
6868
match *self {
6969
Time::Empty => {
7070
panic!("You must supply a timer.")

‎src/proto/h1/role.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ where
8787
}
8888
None => {
8989
debug!("setting h1 header read timeout timer");
90-
*ctx.h1_header_read_timeout_fut =
91-
Some(crate::common::into_pin(ctx.timer.sleep_until(deadline)));
90+
*ctx.h1_header_read_timeout_fut = Some(ctx.timer.sleep_until(deadline));
9291
}
9392
}
9493
}

‎src/proto/h2/ping.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(super) fn channel(ping_pong: PingPong, config: Config, __timer: Time) -> (Re
6161
interval,
6262
timeout: config.keep_alive_timeout,
6363
while_idle: config.keep_alive_while_idle,
64-
sleep: crate::common::into_pin(__timer.sleep(interval)),
64+
sleep: __timer.sleep(interval),
6565
state: KeepAliveState::Init,
6666
timer: __timer,
6767
});

‎src/rt.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ pub trait Executor<Fut> {
2020
/// A timer which provides timer-like functions.
2121
pub trait Timer {
2222
/// Return a future that resolves in `duration` time.
23-
fn sleep(&self, duration: Duration) -> Box<dyn Sleep + Unpin>;
23+
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>>;
2424

2525
/// Return a future that resolves at `deadline`.
26-
fn sleep_until(&self, deadline: Instant) -> Box<dyn Sleep + Unpin>;
26+
fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>>;
2727

2828
/// Reset a future to resolve at `new_deadline` instead.
2929
fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
30-
*sleep = crate::common::into_pin(self.sleep_until(new_deadline));
30+
*sleep = self.sleep_until(new_deadline);
3131
}
3232
}
3333

3434
/// A future returned by a `Timer`.
35-
pub trait Sleep: Send + Sync + Unpin + Future<Output = ()> {}
35+
pub trait Sleep: Send + Sync + Future<Output = ()> {}

0 commit comments

Comments
 (0)
Please sign in to comment.