Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/misc/component-async-tests/src/yield_host.rs
1692 views
1
use super::Ctx;
2
use futures::future;
3
use std::ops::DerefMut;
4
use std::task::Poll;
5
use wasmtime::component::Accessor;
6
7
pub mod bindings {
8
wasmtime::component::bindgen!({
9
path: "wit",
10
world: "yield-host",
11
});
12
}
13
14
impl bindings::local::local::continue_::Host for Ctx {
15
fn set_continue(&mut self, v: bool) {
16
self.continue_ = v;
17
}
18
19
fn get_continue(&mut self) -> bool {
20
self.continue_
21
}
22
}
23
24
impl bindings::local::local::ready::Host for Ctx {
25
fn set_ready(&mut self, ready: bool) {
26
let mut wakers = self.wakers.lock().unwrap();
27
if ready {
28
if let Some(wakers) = wakers.take() {
29
for waker in wakers {
30
waker.wake();
31
}
32
}
33
} else if wakers.is_none() {
34
*wakers = Some(Vec::new());
35
}
36
}
37
}
38
39
impl bindings::local::local::ready::HostWithStore for Ctx {
40
async fn when_ready<T>(accessor: &Accessor<T, Self>) {
41
let wakers = accessor.with(|mut view| view.get().wakers.clone());
42
future::poll_fn(move |cx| {
43
let mut wakers = wakers.lock().unwrap();
44
if let Some(wakers) = wakers.deref_mut() {
45
wakers.push(cx.waker().clone());
46
Poll::Pending
47
} else {
48
Poll::Ready(())
49
}
50
})
51
.await
52
}
53
}
54
55