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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use core::task::Context;
use crate::enums::IDiscriminantProvider;
use crate::option::Option;
use crate::vtable::HasDropVt;
use crate::{IPtrMut, IPtrOwned};
pub use stable_waker::StableWaker;
#[cfg(feature = "unsafe_wakers")]
mod stable_waker {
use core::task::Waker;
use crate::StableLike;
#[crate::stabby]
pub struct StableWaker<'a>(StableLike<&'a Waker, &'a ()>);
impl StableWaker<'_> {
pub fn with_waker<'a, F: FnOnce(&'a Waker) -> U, U>(&'a self, f: F) -> U {
f(self.0.value)
}
}
impl<'a> From<&'a Waker> for StableWaker<'a> {
fn from(value: &'a Waker) -> Self {
unsafe { Self(StableLike::new(value)) }
}
}
}
#[cfg(all(feature = "alloc", not(feature = "unsafe_wakers")))]
mod stable_waker {
use core::{
mem::ManuallyDrop,
task::{RawWaker, RawWakerVTable, Waker},
};
use alloc::sync::Arc;
use crate::StableLike;
#[crate::stabby]
pub struct Tuple2<A, B>(A, B);
#[crate::stabby]
pub struct StableWakerInner {
waker: StableLike<ManuallyDrop<Waker>, Tuple2<*const (), &'static ()>>,
wake_by_ref: StableLike<for<'b> extern "C" fn(&'b Waker), &'static ()>,
drop: StableLike<extern "C" fn(&mut ManuallyDrop<Waker>), &'static ()>,
}
#[crate::stabby]
pub struct StableWaker<'a> {
waker: Arc<StableWakerInner>,
marker: core::marker::PhantomData<&'a ()>,
}
impl StableWaker<'_> {
fn into_waker(waker: Arc<StableWakerInner>) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop);
unsafe fn clone(this: *const ()) -> RawWaker {
Arc::increment_strong_count(this);
RawWaker::new(this, &VTABLE)
}
unsafe fn wake(this: *const ()) {
wake_by_ref(this);
drop(this);
}
unsafe fn wake_by_ref(this: *const ()) {
let this = unsafe { &*(this as *const StableWakerInner) };
(this.wake_by_ref)(&this.waker)
}
unsafe fn drop(this: *const ()) {
Arc::from_raw(this as *const StableWakerInner);
}
let waker = RawWaker::new(Arc::into_raw(waker) as *const _, &VTABLE);
unsafe { Waker::from_raw(waker) }
}
pub fn with_waker<F: FnOnce(&Waker) -> U, U>(&self, f: F) -> U {
let waker = Self::into_waker(self.waker.clone());
f(&waker)
}
}
impl<'a> From<&'a Waker> for StableWaker<'static> {
fn from(value: &'a Waker) -> Self {
extern "C" fn drop_waker(waker: &mut ManuallyDrop<Waker>) {
unsafe { ManuallyDrop::drop(waker) }
}
extern "C" fn wake_by_ref(waker: &Waker) {
waker.wake_by_ref()
}
let waker = unsafe {
StableWakerInner {
waker: StableLike {
value: ManuallyDrop::new(value.clone()),
marker: core::marker::PhantomData,
},
wake_by_ref: StableLike::new(wake_by_ref),
drop: StableLike::new(drop_waker),
}
};
StableWaker {
waker: Arc::new(waker),
marker: core::marker::PhantomData,
}
}
}
}
#[crate::stabby]
pub trait Future {
type Output: IDiscriminantProvider<()>;
extern "C" fn poll<'a>(&'a mut self, waker: StableWaker<'a>) -> Option<Self::Output>;
}
impl<T: core::future::Future> Future for T
where
T::Output: IDiscriminantProvider<()>,
{
type Output = T::Output;
#[allow(improper_ctypes_definitions)]
extern "C" fn poll(&mut self, waker: StableWaker) -> Option<Self::Output> {
waker.with_waker(|waker| {
match core::future::Future::poll(
unsafe { core::pin::Pin::new_unchecked(self) },
&mut Context::from_waker(waker),
) {
core::task::Poll::Ready(v) => Option::Some(v),
core::task::Poll::Pending => Option::None(),
}
})
}
}
impl<'a, Vt: HasDropVt, P: IPtrOwned + IPtrMut, Output: IDiscriminantProvider<()>>
core::future::Future
for crate::Dyn<'a, P, crate::vtable::VTable<StabbyVtableFuture<Output>, Vt>>
{
type Output = Output;
fn poll(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> core::task::Poll<Self::Output> {
unsafe {
let this = core::pin::Pin::get_unchecked_mut(self);
(this.vtable().head.poll)(this.ptr_mut().as_mut(), cx.waker().into())
.match_owned(|v| core::task::Poll::Ready(v), || core::task::Poll::Pending)
}
}
}
impl<'a, Vt: HasDropVt, P: IPtrOwned + IPtrMut, Output: IDiscriminantProvider<()>>
core::future::Future
for crate::Dyn<
'a,
P,
crate::vtable::VtSend<crate::vtable::VTable<StabbyVtableFuture<Output>, Vt>>,
>
{
type Output = Output;
fn poll(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> core::task::Poll<Self::Output> {
unsafe {
let this = core::pin::Pin::get_unchecked_mut(self);
(this.vtable().0.head.poll)(this.ptr_mut().as_mut(), cx.waker().into())
.match_owned(|v| core::task::Poll::Ready(v), || core::task::Poll::Pending)
}
}
}
impl<'a, Vt: HasDropVt, P: IPtrOwned + IPtrMut, Output: IDiscriminantProvider<()>>
core::future::Future
for crate::Dyn<
'a,
P,
crate::vtable::VtSync<crate::vtable::VTable<StabbyVtableFuture<Output>, Vt>>,
>
{
type Output = Output;
fn poll(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> core::task::Poll<Self::Output> {
unsafe {
let this = core::pin::Pin::get_unchecked_mut(self);
(this.vtable().0.head.poll)(this.ptr_mut().as_mut(), cx.waker().into())
.match_owned(|v| core::task::Poll::Ready(v), || core::task::Poll::Pending)
}
}
}
impl<'a, Vt: HasDropVt, P: IPtrOwned + IPtrMut, Output: IDiscriminantProvider<()>>
core::future::Future
for crate::Dyn<
'a,
P,
crate::vtable::VtSync<
crate::vtable::VtSend<crate::vtable::VTable<StabbyVtableFuture<Output>, Vt>>,
>,
>
{
type Output = Output;
fn poll(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> core::task::Poll<Self::Output> {
unsafe {
let this = core::pin::Pin::get_unchecked_mut(self);
(this.vtable().0 .0.head.poll)(this.ptr_mut().as_mut(), cx.waker().into())
.match_owned(|v| core::task::Poll::Ready(v), || core::task::Poll::Pending)
}
}
}
impl<'a, Vt: HasDropVt, P: IPtrOwned + IPtrMut, Output: IDiscriminantProvider<()>>
core::future::Future
for crate::Dyn<
'a,
P,
crate::vtable::VtSend<
crate::vtable::VtSync<crate::vtable::VTable<StabbyVtableFuture<Output>, Vt>>,
>,
>
{
type Output = Output;
fn poll(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> core::task::Poll<Self::Output> {
unsafe {
let this = core::pin::Pin::get_unchecked_mut(self);
(this.vtable().0 .0.head.poll)(this.ptr_mut().as_mut(), cx.waker().into())
.match_owned(|v| core::task::Poll::Ready(v), || core::task::Poll::Pending)
}
}
}