Path: blob/main/crates/misc/component-async-tests/src/yield_host.rs
1692 views
use super::Ctx;1use futures::future;2use std::ops::DerefMut;3use std::task::Poll;4use wasmtime::component::Accessor;56pub mod bindings {7wasmtime::component::bindgen!({8path: "wit",9world: "yield-host",10});11}1213impl bindings::local::local::continue_::Host for Ctx {14fn set_continue(&mut self, v: bool) {15self.continue_ = v;16}1718fn get_continue(&mut self) -> bool {19self.continue_20}21}2223impl bindings::local::local::ready::Host for Ctx {24fn set_ready(&mut self, ready: bool) {25let mut wakers = self.wakers.lock().unwrap();26if ready {27if let Some(wakers) = wakers.take() {28for waker in wakers {29waker.wake();30}31}32} else if wakers.is_none() {33*wakers = Some(Vec::new());34}35}36}3738impl bindings::local::local::ready::HostWithStore for Ctx {39async fn when_ready<T>(accessor: &Accessor<T, Self>) {40let wakers = accessor.with(|mut view| view.get().wakers.clone());41future::poll_fn(move |cx| {42let mut wakers = wakers.lock().unwrap();43if let Some(wakers) = wakers.deref_mut() {44wakers.push(cx.waker().clone());45Poll::Pending46} else {47Poll::Ready(())48}49})50.await51}52}535455