Skip to main content

wasm_bindgen_futures/
lib.rs

1//! Bridging the gap between Rust Futures and JavaScript Promises.
2//!
3//! This crate is now a thin shim re-exporting from [`js_sys::futures`].
4//! The implementation has been moved into `js-sys` so that
5//! [`js_sys::Promise`] can implement [`core::future::IntoFuture`] directly,
6//! enabling `promise.await` without any wrapper type.
7//!
8//! All public items (`JsFuture`, `spawn_local`, `future_to_promise`,
9//! `future_to_promise_typed`) are re-exported unchanged for backwards
10//! compatibility.
11
12#![cfg_attr(not(feature = "std"), no_std)]
13
14pub use js_sys::futures::{future_to_promise, future_to_promise_typed, spawn_local, JsFuture};
15
16#[cfg(feature = "futures-core-03-stream")]
17pub use js_sys::futures::stream;
18
19#[cfg(not(target_feature = "atomics"))]
20#[allow(deprecated)]
21pub use js_sys::futures::jspi_block_on_promise;
22
23#[cfg(all(target_os = "emscripten", wasm_bindgen_unstable_tokio))]
24pub mod tokio;
25
26/// Stand-in for unsupported targets: `#[wasm_bindgen(experimental_tokio)]`
27/// expansions still resolve, and the unsatisfiable bound reports why at the
28/// attribute instead of as an unresolved path.
29#[cfg(not(all(target_os = "emscripten", wasm_bindgen_unstable_tokio)))]
30#[doc(hidden)]
31pub mod tokio {
32    #[diagnostic::on_unimplemented(
33        message = "`#[wasm_bindgen(experimental_tokio)]` is only supported on the \
34                   `wasm32-unknown-emscripten` target with `--cfg wasm_bindgen_unstable_tokio`",
35        label = "this async export is driven on a tokio event loop"
36    )]
37    pub trait ExperimentalTokioSupported {}
38
39    pub fn schedule<F: ExperimentalTokioSupported, C>(_future: F, _on_complete: C) {
40        unreachable!()
41    }
42
43    pub fn schedule_isolated<F: ExperimentalTokioSupported, C>(_future: F, _on_complete: C) {
44        unreachable!()
45    }
46
47    pub fn block_on<F: ExperimentalTokioSupported + core::future::Future>(_future: F) -> F::Output {
48        unreachable!()
49    }
50
51    pub fn block_on_isolated<F: ExperimentalTokioSupported + core::future::Future>(
52        _future: F,
53    ) -> F::Output {
54        unreachable!()
55    }
56}
57
58pub use js_sys;
59pub use wasm_bindgen;