From baadcc150f54012668f5484f5fbe8dd2516ebb10 Mon Sep 17 00:00:00 2001 From: edwloef Date: Thu, 13 Mar 2025 23:12:44 +0100 Subject: [PATCH] don't use futures-executor when it's not the default executor --- Cargo.lock | 1 + Cargo.toml | 6 ++++-- examples/integration/Cargo.toml | 3 +++ futures/src/backend/native/async_std.rs | 4 ++++ futures/src/backend/native/smol.rs | 4 ++++ futures/src/backend/native/thread_pool.rs | 4 ++++ futures/src/backend/native/tokio.rs | 7 +++++++ futures/src/backend/null.rs | 6 ++++++ futures/src/backend/wasm/wasm_bindgen.rs | 4 ++++ futures/src/executor.rs | 3 +++ runtime/Cargo.toml | 1 - winit/src/program.rs | 10 +--------- 12 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9606b9a6..ae3436ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2777,6 +2777,7 @@ version = "0.1.0" dependencies = [ "console_error_panic_hook", "console_log", + "futures", "iced_wgpu", "iced_widget", "iced_winit", diff --git a/Cargo.toml b/Cargo.toml index c5e8f865..f5527e53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ all-features = true maintenance = { status = "actively-developed" } [features] -default = ["wgpu", "tiny-skia", "auto-detect-theme"] +default = ["wgpu", "tiny-skia", "auto-detect-theme", "futures-executor"] # Enables the `wgpu` GPU-accelerated renderer backend wgpu = ["iced_renderer/wgpu", "iced_widget/wgpu"] # Enables the `tiny-skia` software renderer backend @@ -43,6 +43,8 @@ markdown = ["iced_widget/markdown"] lazy = ["iced_widget/lazy"] # Enables a debug view in native platforms (press F12) debug = ["iced_winit/debug"] +# Enables `futures-executor` as the `executor::Default` on native platforms +futures-executor = ["iced_futures/thread-pool"] # Enables `tokio` as the `executor::Default` on native platforms tokio = ["iced_futures/tokio"] # Enables `async-std` as the `executor::Default` on native platforms @@ -150,7 +152,7 @@ bytemuck = { version = "1.0", features = ["derive"] } bytes = "1.6" cosmic-text = "0.13" dark-light = "2.0" -futures = "0.3" +futures = { version = "0.3", default-features = false, features = ["std", "async-await"] } glam = "0.25" cryoglyph = { git = "https://github.com/iced-rs/cryoglyph.git", rev = "be2defe4a13fd7c97c6f4c81e8e085463eb578dc" } guillotiere = "0.6" diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 3bdf9408..2b5f99e4 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -12,6 +12,9 @@ iced_wgpu.workspace = true iced_widget.workspace = true iced_widget.features = ["wgpu"] +futures.workspace = true +futures.features = ["thread-pool"] + [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tracing-subscriber = "0.3" diff --git a/futures/src/backend/native/async_std.rs b/futures/src/backend/native/async_std.rs index be258b26..9acafd9c 100644 --- a/futures/src/backend/native/async_std.rs +++ b/futures/src/backend/native/async_std.rs @@ -13,6 +13,10 @@ impl crate::Executor for Executor { fn spawn(&self, future: impl Future + Send + 'static) { let _ = async_std::task::spawn(future); } + + fn block_on(future: impl Future + 'static) { + async_std::task::block_on(future); + } } pub mod time { diff --git a/futures/src/backend/native/smol.rs b/futures/src/backend/native/smol.rs index 9ac6a27d..27998e8f 100644 --- a/futures/src/backend/native/smol.rs +++ b/futures/src/backend/native/smol.rs @@ -12,6 +12,10 @@ impl crate::Executor for Executor { fn spawn(&self, future: impl Future + Send + 'static) { smol::spawn(future).detach(); } + + fn block_on(future: impl Future + 'static) { + smol::block_on(future); + } } pub mod time { diff --git a/futures/src/backend/native/thread_pool.rs b/futures/src/backend/native/thread_pool.rs index a90cc53a..92004bc7 100644 --- a/futures/src/backend/native/thread_pool.rs +++ b/futures/src/backend/native/thread_pool.rs @@ -11,6 +11,10 @@ impl crate::Executor for Executor { fn spawn(&self, future: impl Future + Send + 'static) { self.spawn_ok(future); } + + fn block_on(future: impl Future + 'static) { + futures::executor::block_on(future); + } } pub mod time { diff --git a/futures/src/backend/native/tokio.rs b/futures/src/backend/native/tokio.rs index 911d788c..0670c5ee 100644 --- a/futures/src/backend/native/tokio.rs +++ b/futures/src/backend/native/tokio.rs @@ -17,6 +17,13 @@ impl crate::Executor for Executor { let _guard = tokio::runtime::Runtime::enter(self); f() } + + fn block_on(future: impl Future + 'static) { + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(future); + } } pub mod time { diff --git a/futures/src/backend/null.rs b/futures/src/backend/null.rs index f31415b9..151d5587 100644 --- a/futures/src/backend/null.rs +++ b/futures/src/backend/null.rs @@ -14,6 +14,12 @@ impl crate::Executor for Executor { #[cfg(target_arch = "wasm32")] fn spawn(&self, _future: impl Future + 'static) {} + + #[cfg(not(target_arch = "wasm32"))] + fn block_on(_future: impl Future + 'static) {} + + #[cfg(target_arch = "wasm32")] + fn block_on(_future: impl Future + 'static) {} } pub mod time { diff --git a/futures/src/backend/wasm/wasm_bindgen.rs b/futures/src/backend/wasm/wasm_bindgen.rs index 4811e7f4..667bf2fa 100644 --- a/futures/src/backend/wasm/wasm_bindgen.rs +++ b/futures/src/backend/wasm/wasm_bindgen.rs @@ -12,6 +12,10 @@ impl crate::Executor for Executor { fn spawn(&self, future: impl futures::Future + 'static) { wasm_bindgen_futures::spawn_local(future); } + + fn block_on(future: impl futures::Future + 'static) { + wasm_bindgen_futures::spawn_local(future); + } } pub mod time { diff --git a/futures/src/executor.rs b/futures/src/executor.rs index 9c14a2c9..22ed4212 100644 --- a/futures/src/executor.rs +++ b/futures/src/executor.rs @@ -20,4 +20,7 @@ pub trait Executor: Sized { fn enter(&self, f: impl FnOnce() -> R) -> R { f() } + + /// Runs the future on the current thread, blocking it until it is completed. + fn block_on(future: impl Future + 'static); } diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index fc212ef8..35704e0f 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -21,7 +21,6 @@ multi-window = [] bytes.workspace = true iced_core.workspace = true iced_futures.workspace = true -iced_futures.features = ["thread-pool"] raw-window-handle.workspace = true sipper.workspace = true diff --git a/winit/src/program.rs b/winit/src/program.rs index 6904c02a..ee921adb 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -662,15 +662,7 @@ async fn run_instance( } }; - #[cfg(not(target_arch = "wasm32"))] - crate::futures::futures::executor::block_on( - create_compositor, - ); - - #[cfg(target_arch = "wasm32")] - { - wasm_bindgen_futures::spawn_local(create_compositor); - } + P::Executor::block_on(create_compositor); continue; }