From 04086a90c9e933ebfb42de378054e1115b33529d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 20 Jan 2020 05:43:09 +0100 Subject: Implement `WasmBindgen` executor and reorganize --- futures/Cargo.toml | 3 +++ futures/src/executor.rs | 12 +++++++++--- futures/src/executor/wasm_bindgen.rs | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 futures/src/executor/wasm_bindgen.rs (limited to 'futures') diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 13c2d6b7..91860e1e 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -27,3 +27,6 @@ features = ["rt-core"] [dependencies.async-std] version = "1.0" optional = true + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen-futures = "0.4" diff --git a/futures/src/executor.rs b/futures/src/executor.rs index 144a41f8..b2ff043e 100644 --- a/futures/src/executor.rs +++ b/futures/src/executor.rs @@ -4,23 +4,29 @@ mod null; #[cfg(feature = "thread-pool")] mod thread_pool; -#[cfg(feature = "thread-pool")] -pub use thread_pool::ThreadPool; - #[cfg(feature = "tokio")] mod tokio; #[cfg(feature = "async-std")] mod async_std; +#[cfg(target_arch = "wasm32")] +mod wasm_bindgen; + pub use null::Null; +#[cfg(feature = "thread-pool")] +pub use thread_pool::ThreadPool; + #[cfg(feature = "tokio")] pub use self::tokio::Tokio; #[cfg(feature = "async-std")] pub use self::async_std::AsyncStd; +#[cfg(target_arch = "wasm32")] +pub use wasm_bindgen::WasmBindgen; + use futures::Future; pub trait Executor: Sized { diff --git a/futures/src/executor/wasm_bindgen.rs b/futures/src/executor/wasm_bindgen.rs new file mode 100644 index 00000000..70a8ea8e --- /dev/null +++ b/futures/src/executor/wasm_bindgen.rs @@ -0,0 +1,17 @@ +use crate::Executor; + +#[derive(Debug)] +pub struct WasmBindgen; + +impl Executor for WasmBindgen { + fn new() -> Result { + Ok(Self) + } + + fn spawn( + &self, + future: impl futures::Future + Send + 'static, + ) { + wasm_bindgen_futures::spawn_local(future); + } +} -- cgit