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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, RwLock};
use std::task::{Context, Poll, Waker};
type Waiter<Env, T> = Box<dyn Fn(&mut Env, &T)>;
pub struct Promise<Env, T> {
inner: Arc<RwLock<Inner<T>>>,
waiters: Arc<RwLock<Vec<Waiter<Env, T>>>>,
}
pub struct Complete<T> {
inner: Arc<RwLock<Inner<T>>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cancelled;
struct Inner<T> {
value: Option<Arc<T>>,
waker: Option<Waker>,
}
pub fn promise<Env, T>() -> (Complete<T>, Promise<Env, T>) {
let inner = Arc::new(RwLock::new(Inner {
value: None,
waker: None,
}));
let promise = Promise {
inner: inner.clone(),
waiters: Arc::new(RwLock::new(Vec::new())),
};
let complete = Complete { inner };
(complete, promise)
}
impl<T> Complete<T> {
pub fn fulfill(&self, val: T) {
let mut inner = self.inner.write().unwrap();
inner.value = Some(Arc::new(val));
if let Some(waker) = inner.waker.take() {
waker.wake()
}
}
}
impl<T> Complete<Result<T, Cancelled>> {
pub fn cancel(&mut self) {
self.fulfill(Err(Cancelled))
}
}
impl<E, T> Complete<Result<T, E>> {
pub fn ok(&mut self, val: T) {
self.fulfill(Ok(val))
}
pub fn err(&mut self, e: E) {
self.fulfill(Err(e))
}
}
impl<Env, T> Promise<Env, T> {
pub fn on_fulfill<F: Fn(&mut Env, &T) + 'static>(&mut self, f: F) {
let mut waiters = self.waiters.write().unwrap();
waiters.push(Box::new(f));
}
}
impl<Env, T> Promise<Env, Result<T, Cancelled>> {
pub fn on_cancel<F: Fn(&mut Env) + 'static>(&mut self, f: F) {
self.on_err(move |env, _| f(env))
}
}
impl<Env, E, T> Promise<Env, Result<T, E>> {
pub fn on_ok<F: Fn(&mut Env, &T) + 'static>(&mut self, f: F) {
self.on_fulfill(move |env, r| {
if let Ok(val) = r {
f(env, val)
}
})
}
pub fn on_err<F: Fn(&mut Env, &E) + 'static>(&mut self, f: F) {
self.on_fulfill(move |env, r| {
if let Err(e) = r {
f(env, e)
}
})
}
}
pub trait Give<Env> {
fn give(&self, env: &mut Env) -> bool;
}
impl<Env, T> Give<Env> for Promise<Env, T> {
fn give(&self, env: &mut Env) -> bool {
let inner = self.inner.read().unwrap();
if let Some(value) = &inner.value {
let mut waiters = self.waiters.write().unwrap();
for waiter in waiters.iter() {
waiter(env, value);
}
waiters.clear();
true
} else {
false
}
}
}
impl<Env, T> Clone for Promise<Env, T> {
fn clone(&self) -> Self {
Promise {
inner: self.inner.clone(),
waiters: self.waiters.clone(),
}
}
}
impl<Env, P: Give<Env>> Give<Env> for &P {
fn give(&self, env: &mut Env) -> bool {
(*self).give(env)
}
}
impl<Env, T> Future for Promise<Env, T> {
type Output = Arc<T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut inner = self.inner.write().unwrap();
match inner.value {
Some(ref v) => Poll::Ready(v.clone()),
None => {
inner.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
}
pub struct Promises<'a, Env> {
ps: Vec<Box<dyn Give<Env> + 'a>>,
}
impl<'a, Env> Promises<'a, Env> {
pub fn new() -> Self {
Promises { ps: Vec::new() }
}
pub fn push(&mut self, p: Box<dyn Give<Env> + 'a>) {
self.ps.push(p);
}
pub fn give_all(&mut self, env: &mut Env) {
self.ps.retain(|p| !p.give(env));
}
}
|