diff --git a/futures/src/backend/native/smol.rs b/futures/src/backend/native/smol.rs index 9ac6a27d..3080d5ff 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(&self, future: impl Future) -> T { + 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..a71ab8c2 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(&self, future: impl Future) -> T { + 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..93600635 100644 --- a/futures/src/backend/native/tokio.rs +++ b/futures/src/backend/native/tokio.rs @@ -17,6 +17,10 @@ impl crate::Executor for Executor { let _guard = tokio::runtime::Runtime::enter(self); f() } + + fn block_on(&self, future: impl Future) -> T { + self.block_on(future) + } } pub mod time { diff --git a/futures/src/backend/null.rs b/futures/src/backend/null.rs index 59b740e3..67a17d68 100644 --- a/futures/src/backend/null.rs +++ b/futures/src/backend/null.rs @@ -11,6 +11,11 @@ impl crate::Executor for Executor { } fn spawn(&self, _future: impl Future + MaybeSend + 'static) {} + + #[cfg(not(target_arch = "wasm32"))] + fn block_on(&self, _future: impl Future) -> T { + unimplemented!() + } } pub mod time { diff --git a/futures/src/executor.rs b/futures/src/executor.rs index 9c14a2c9..00fecb1a 100644 --- a/futures/src/executor.rs +++ b/futures/src/executor.rs @@ -11,6 +11,10 @@ pub trait Executor: Sized { /// Spawns a future in the [`Executor`]. fn spawn(&self, future: impl Future + MaybeSend + 'static); + /// Runs a future to completion in the current thread within the [`Executor`]. + #[cfg(not(target_arch = "wasm32"))] + fn block_on(&self, future: impl Future) -> T; + /// Runs the given closure inside the [`Executor`]. /// /// Some executors, like `tokio`, require some global state to be in place diff --git a/futures/src/runtime.rs b/futures/src/runtime.rs index 72252458..e25ba1d7 100644 --- a/futures/src/runtime.rs +++ b/futures/src/runtime.rs @@ -50,12 +50,10 @@ where self.executor.enter(f) } - /// Spawns a [`Future`] in the [`Runtime`]. - pub fn spawn( - &mut self, - future: impl Future + MaybeSend + 'static, - ) { - self.executor.spawn(future); + /// Runs a future to completion in the current thread within the [`Runtime`]. + #[cfg(not(target_arch = "wasm32"))] + pub fn block_on(&mut self, future: impl Future) -> T { + self.executor.block_on(future) } /// Runs a [`Stream`] in the [`Runtime`] until completion. diff --git a/graphics/src/compositor.rs b/graphics/src/compositor.rs index f0f67607..e9063678 100644 --- a/graphics/src/compositor.rs +++ b/graphics/src/compositor.rs @@ -10,7 +10,7 @@ use thiserror::Error; use std::borrow::Cow; /// A graphics compositor that can draw to windows. -pub trait Compositor: Sized + MaybeSend { +pub trait Compositor: Sized { /// The iced renderer of the backend. type Renderer; @@ -21,7 +21,7 @@ pub trait Compositor: Sized + MaybeSend { fn new( settings: Settings, compatible_window: W, - ) -> impl Future> + MaybeSend { + ) -> impl Future> { Self::with_backend(settings, compatible_window, None) } @@ -33,7 +33,7 @@ pub trait Compositor: Sized + MaybeSend { _settings: Settings, _compatible_window: W, _backend: Option<&str>, - ) -> impl Future> + MaybeSend; + ) -> impl Future>; /// Creates a [`Self::Renderer`] for the [`Compositor`]. fn create_renderer(&self) -> Self::Renderer; diff --git a/winit/src/program.rs b/winit/src/program.rs index 77b4f9d7..f47dfdf5 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -18,7 +18,7 @@ use crate::futures::futures::channel::oneshot; use crate::futures::futures::task; use crate::futures::futures::{Future, StreamExt}; use crate::futures::subscription::{self, Subscription}; -use crate::futures::{Executor, MaybeSend, Runtime}; +use crate::futures::{Executor, Runtime}; use crate::graphics; use crate::graphics::{Compositor, compositor}; use crate::runtime::Debug; @@ -149,7 +149,7 @@ pub fn run( ) -> Result<(), Error> where P: Program + 'static, - C: Compositor + MaybeSend + 'static, + C: Compositor + 'static, P::Theme: theme::Base, { use winit::event_loop::EventLoop; @@ -560,7 +560,7 @@ async fn run_instance( default_fonts: Vec>, ) where P: Program + 'static, - C: Compositor + MaybeSend + 'static, + C: Compositor + 'static, P::Theme: theme::Base, { use winit::event; @@ -636,7 +636,11 @@ async fn run_instance( } }; - runtime.spawn(create_compositor); + #[cfg(target_arch = "wasm32")] + wasm_bindgen_futures::spawn_local(create_compositor); + + #[cfg(not(target_arch = "wasm32"))] + runtime.block_on(create_compositor); match compositor_receiver .await @@ -648,7 +652,7 @@ async fn run_instance( Err(error) => { let _ = control_sender .start_send(Control::Crash(error.into())); - break; + continue; } } }