Skip to content

Commit c7d9a07

Browse files
committed
reactor: don't use async fn for sleep
1 parent 72b03a6 commit c7d9a07

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

src/implementors/async_io.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use async_io::{Async, Timer};
33
use async_trait::async_trait;
44
use futures_core::Stream;
55
use std::{
6+
future::Future,
67
io,
78
net::{SocketAddr, TcpStream},
9+
pin::Pin,
10+
task::{self, Context, Poll},
811
time::{Duration, Instant},
912
};
1013

@@ -21,8 +24,8 @@ impl Reactor for AsyncIO {
2124
Async::new(socket)
2225
}
2326

24-
async fn sleep(&self, dur: Duration) {
25-
Timer::after(dur).await;
27+
fn sleep(&self, dur: Duration) -> impl Future<Output = ()> {
28+
TimerTask(Timer::after(dur))
2629
}
2730

2831
fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> {
@@ -34,6 +37,17 @@ impl Reactor for AsyncIO {
3437
}
3538
}
3639

40+
pub(crate) struct TimerTask<T: Future + Unpin>(pub(crate) T);
41+
42+
impl<T: Future + Unpin> Future for TimerTask<T> {
43+
type Output = ();
44+
45+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
46+
task::ready!(Pin::new(&mut self.0).poll(cx));
47+
Poll::Ready(())
48+
}
49+
}
50+
3751
#[cfg(test)]
3852
mod tests {
3953
use super::*;

src/implementors/smol.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! smol implementation of async runtime definition traits
22
3-
use crate::{AsyncIOHandle, Executor, IOHandle, Reactor, Runtime, RuntimeKit, Task, sys::IO};
3+
use crate::{
4+
AsyncIOHandle, Executor, IOHandle, Reactor, Runtime, RuntimeKit, Task, TimerTask, sys::IO,
5+
};
46
use async_trait::async_trait;
57
use futures_core::Stream;
68
use smol::{Async, Timer};
@@ -83,8 +85,8 @@ impl Reactor for Smol {
8385
Async::new(socket)
8486
}
8587

86-
async fn sleep(&self, dur: Duration) {
87-
Timer::after(dur).await;
88+
fn sleep(&self, dur: Duration) -> impl Future<Output = ()> {
89+
TimerTask(Timer::after(dur))
8890
}
8991

9092
fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> {

src/implementors/tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ impl Reactor for Tokio {
126126
}
127127
}
128128

129-
async fn sleep(&self, dur: Duration) {
130-
tokio::time::sleep(dur).await;
129+
fn sleep(&self, dur: Duration) -> impl Future<Output = ()> {
130+
tokio::time::sleep(dur)
131131
}
132132

133133
fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> {

src/runtime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ impl<RK: RuntimeKit + Sync + 'static> Reactor for Runtime<RK> {
5858
self.kit.register(socket)
5959
}
6060

61-
async fn sleep(&self, dur: Duration) {
62-
self.kit.sleep(dur).await;
61+
fn sleep(&self, dur: Duration) -> impl Future<Output = ()> {
62+
self.kit.sleep(dur)
6363
}
6464

6565
fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> {
@@ -119,8 +119,8 @@ impl<E: Executor + Sync, R: Reactor + Sync> Reactor for RuntimeParts<E, R> {
119119
self.reactor.register(socket)
120120
}
121121

122-
async fn sleep(&self, dur: Duration) {
123-
self.reactor.sleep(dur).await;
122+
fn sleep(&self, dur: Duration) -> impl Future<Output = ()> {
123+
self.reactor.sleep(dur)
124124
}
125125

126126
fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> {

src/traits/reactor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ pub trait Reactor {
2323
Self: Sized;
2424

2525
/// Sleep for the given duration
26-
async fn sleep(&self, dur: Duration);
26+
fn sleep(&self, dur: Duration) -> impl Future<Output = ()>
27+
where
28+
Self: Sized;
2729

2830
/// Stream that yields at every given interval
2931
fn interval(&self, dur: Duration) -> impl Stream<Item = Instant>

0 commit comments

Comments
 (0)