From 90690702e1e4abab804ec91e8ff4183824bec436 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 20 Jan 2020 04:47:36 +0100 Subject: Add `Application::Executor` associated type --- src/application.rs | 14 ++++++++++++-- src/lib.rs | 22 +++++++++++++++------- src/native.rs | 2 ++ src/native/executor.rs | 23 +++++++++++++++++++++++ src/sandbox.rs | 3 ++- 5 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 src/native/executor.rs (limited to 'src') diff --git a/src/application.rs b/src/application.rs index b940cc17..3a526f1b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,4 +1,4 @@ -use crate::{window, Command, Element, Settings, Subscription}; +use crate::{window, Command, Element, Executor, Settings, Subscription}; /// An interactive cross-platform application. /// @@ -19,7 +19,7 @@ use crate::{window, Command, Element, Settings, Subscription}; /// before](index.html#overview). We just need to fill in the gaps: /// /// ```no_run -/// use iced::{button, Application, Button, Column, Command, Element, Settings, Text}; +/// use iced::{button, executor, Application, Button, Column, Command, Element, Settings, Text}; /// /// pub fn main() { /// Counter::run(Settings::default()) @@ -39,6 +39,7 @@ use crate::{window, Command, Element, Settings, Subscription}; /// } /// /// impl Application for Counter { +/// type Executor = executor::Null; /// type Message = Message; /// /// fn new() -> (Self, Command) { @@ -80,6 +81,14 @@ use crate::{window, Command, Element, Settings, Subscription}; /// } /// ``` pub trait Application: Sized { + /// The [`Executor`] that will run commands and subscriptions. + /// + /// The [`executor::Default`] can be a good starting point! + /// + /// [`Executor`]: trait.Executor.html + /// [`executor::Default`]: executor/struct.Default.html + type Executor: Executor; + /// The type of __messages__ your [`Application`] will produce. /// /// [`Application`]: trait.Application.html @@ -185,6 +194,7 @@ where A: Application, { type Renderer = iced_wgpu::Renderer; + type Executor = A::Executor; type Message = A::Message; fn new() -> (Self, Command) { diff --git a/src/lib.rs b/src/lib.rs index 759dea2f..18dfc098 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -180,18 +180,26 @@ #![deny(unsafe_code)] #![deny(rust_2018_idioms)] mod application; -#[cfg(target_arch = "wasm32")] -#[path = "web.rs"] -mod platform; -#[cfg(not(target_arch = "wasm32"))] -#[path = "native.rs"] -mod platform; mod sandbox; +#[cfg(not(target_arch = "wasm32"))] +mod native; + +#[cfg(not(target_arch = "wasm32"))] +pub use native::*; + +#[cfg(target_arch = "wasm32")] +mod web; + +#[cfg(target_arch = "wasm32")] +pub use web::*; + pub mod settings; pub mod window; +#[doc(no_inline)] +pub use executor::Executor; + pub use application::Application; -pub use platform::*; pub use sandbox::Sandbox; pub use settings::Settings; diff --git a/src/native.rs b/src/native.rs index 35441a3e..86ccffab 100644 --- a/src/native.rs +++ b/src/native.rs @@ -3,6 +3,8 @@ pub use iced_winit::{ Space, Subscription, Vector, VerticalAlignment, }; +pub mod executor; + pub mod widget { //! Display information and interactive controls in your application. //! diff --git a/src/native/executor.rs b/src/native/executor.rs new file mode 100644 index 00000000..68a1d280 --- /dev/null +++ b/src/native/executor.rs @@ -0,0 +1,23 @@ +//! Choose your preferred executor to power your application. +pub use iced_winit::{executor::Null, Executor}; +use iced_winit::{executor::ThreadPool, futures}; + +/// The default cross-platform executor. +/// +/// - On native platforms, it will use a `ThreadPool`. +/// - On the Web, it will use `wasm-bindgen-futures::spawn_local`. +#[derive(Debug)] +pub struct Default(ThreadPool); + +impl Executor for Default { + fn new() -> Result { + Ok(Default(ThreadPool::new()?)) + } + + fn spawn( + &self, + future: impl futures::Future + Send + 'static, + ) { + self.0.spawn(future); + } +} diff --git a/src/sandbox.rs b/src/sandbox.rs index dda4c3f5..2c0332ff 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -1,4 +1,4 @@ -use crate::{Application, Command, Element, Settings, Subscription}; +use crate::{executor, Application, Command, Element, Settings, Subscription}; /// A sandboxed [`Application`]. /// @@ -133,6 +133,7 @@ impl Application for T where T: Sandbox, { + type Executor = executor::Null; type Message = T::Message; fn new() -> (Self, Command) { -- cgit