-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfuture.rs
More file actions
101 lines (81 loc) · 2.22 KB
/
future.rs
File metadata and controls
101 lines (81 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#![allow(unused)]
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
// Hello
async fn hello() {
println!("hello");
}
// Return type of a async function is a compiler-generated anonymous type
// A type that implements Future<Output = i32>
fn hello_fut() -> impl Future<Output = ()> {
async {
println!("hello");
}
}
struct Hello;
impl Future for Hello {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
println!("hello");
Poll::Ready(())
}
}
fn hello_impl_fut() -> Hello {
Hello
}
// Sleep
async fn sleep(dt: u64) {
tokio::time::sleep(Duration::from_millis(dt)).await;
}
fn sleep_fut(dt: u64) -> impl Future<Output = ()> {
async move {
tokio::time::sleep(Duration::from_millis(dt)).await;
}
}
fn sleep_impl_fut(dt: u64) -> Sleep {
Sleep {
time: Instant::now() + Duration::from_millis(dt),
}
}
#[derive(Debug)]
struct Sleep {
pub time: Instant,
}
impl Future for Sleep {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
if Instant::now() >= self.time {
Poll::Ready(())
} else {
// waker must be called when returning Pending
// Forgetting to do this results in the task hanging indefinitely
// This wakes the executor on every loop which wastes CPU cycles
// println!("pending");
// cx.waker().wake_by_ref();
let waker = cx.waker().clone();
let time = self.time;
// Thread is spawned per call to sleep
std::thread::spawn(move || {
let now = Instant::now();
if now < time {
std::thread::sleep(time - now);
}
waker.wake();
});
Poll::Pending
}
}
}
#[tokio::main]
async fn main() {
// Compiler generated anonymous type
let fut: _ = hello_fut();
// Need to await for output
println!("hello_fut: {:?}", fut.await);
let fut = hello_impl_fut();
println!("hello_impl_fut: {:?}", fut.await);
sleep_impl_fut(1000).await;
println!("⏰");
}