|
1 | 1 | //! Async event loop support. |
2 | 2 | //! |
3 | | -//! The way to use this is to call [`block_on()`]. Inside the future, [`Reactor::current`] |
4 | | -//! will give an instance of the [`Reactor`] running the event loop, which can be |
5 | | -//! to [`AsyncPollable::wait_for`] instances of |
| 3 | +//! On WASI 0.2 the way to use this is to call [`block_on()`]. Inside the |
| 4 | +//! future, [`Reactor::current`] will give an instance of the [`Reactor`] |
| 5 | +//! running the event loop, which can be used to [`AsyncPollable::wait_for`] |
| 6 | +//! instances of |
6 | 7 | //! [`wasip2::Pollable`](https://docs.rs/wasi/latest/wasi/io/poll/struct.Pollable.html). |
7 | 8 | //! This will automatically wait for the futures to resolve, and call the |
8 | 9 | //! necessary wakers to work. |
| 10 | +//! |
| 11 | +//! On WASI 0.3 [`block_on`] can be used to drive a future, but an async |
| 12 | +//! function can also be directly exported and will be driven by the host. |
9 | 13 |
|
10 | 14 | #![deny(missing_debug_implementations, nonstandard_style)] |
11 | 15 | #![warn(missing_docs, unreachable_pub)] |
12 | 16 |
|
| 17 | +pub use ::async_task::Task; |
| 18 | + |
| 19 | +#[cfg(target_env = "p2")] |
13 | 20 | mod block_on; |
| 21 | +#[cfg(target_env = "p2")] |
14 | 22 | mod reactor; |
15 | 23 |
|
16 | | -pub use ::async_task::Task; |
| 24 | +#[cfg(target_env = "p2")] |
17 | 25 | pub use block_on::block_on; |
| 26 | +#[cfg(target_env = "p2")] |
18 | 27 | pub use reactor::{AsyncPollable, Reactor, WaitFor}; |
| 28 | +#[cfg(target_env = "p2")] |
19 | 29 | use std::cell::RefCell; |
20 | 30 |
|
21 | 31 | // There are no threads in WASI 0.2, so this is just a safe way to thread a single reactor to all |
22 | 32 | // use sites in the background. |
| 33 | +#[cfg(target_env = "p2")] |
23 | 34 | std::thread_local! { |
24 | 35 | pub(crate) static REACTOR: RefCell<Option<Reactor>> = const { RefCell::new(None) }; |
25 | 36 | } |
26 | 37 |
|
27 | 38 | /// Spawn a `Future` as a `Task` on the current `Reactor`. |
28 | 39 | /// |
29 | 40 | /// Panics if called from outside `block_on`. |
| 41 | +#[cfg(target_env = "p2")] |
30 | 42 | pub fn spawn<F, T>(fut: F) -> Task<T> |
31 | 43 | where |
32 | 44 | F: std::future::Future<Output = T> + 'static, |
33 | 45 | T: 'static, |
34 | 46 | { |
35 | 47 | Reactor::current().spawn(fut) |
36 | 48 | } |
| 49 | + |
| 50 | +#[cfg(target_env = "p3")] |
| 51 | +pub use ::async_task::Runnable; |
| 52 | +#[cfg(target_env = "p3")] |
| 53 | +pub use wasip3::wit_bindgen::block_on; |
| 54 | + |
| 55 | +/// Spawn a `Future` as a `Task` on the WASI 0.3 async runtime. |
| 56 | +#[cfg(target_env = "p3")] |
| 57 | +pub fn spawn<F, T>(fut: F) -> Task<T> |
| 58 | +where |
| 59 | + F: std::future::Future<Output = T> + 'static, |
| 60 | + T: 'static, |
| 61 | +{ |
| 62 | + let (runnable, task) = async_task::spawn_local(fut, |runnable: Runnable| { |
| 63 | + // Scheduling the task is accomplished by spawning a future which |
| 64 | + // executes the `run` method. |
| 65 | + wasip3::spawn_local(async move { |
| 66 | + let _ = runnable.run(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + runnable.schedule(); |
| 70 | + task |
| 71 | +} |
0 commit comments