diff options
author | 2020-02-06 03:56:21 +0100 | |
---|---|---|
committer | 2020-02-06 03:56:21 +0100 | |
commit | ad500441afc355f0b8ca2a463248d350d74f0f20 (patch) | |
tree | 66d58de7d3ab822db12118e22554ad883da3cc30 | |
parent | e8316b208705910958152b2ef6c4c5d7110b4e6c (diff) | |
download | iced-ad500441afc355f0b8ca2a463248d350d74f0f20.tar.gz iced-ad500441afc355f0b8ca2a463248d350d74f0f20.tar.bz2 iced-ad500441afc355f0b8ca2a463248d350d74f0f20.zip |
Allow switching `executor::Default` with features
-rw-r--r-- | Cargo.toml | 11 | ||||
-rw-r--r-- | examples/pokedex/Cargo.toml | 3 | ||||
-rw-r--r-- | examples/pokedex/src/main.rs | 7 | ||||
-rw-r--r-- | examples/todos/Cargo.toml | 3 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 7 | ||||
-rw-r--r-- | src/executor.rs | 31 |
6 files changed, 34 insertions, 28 deletions
@@ -12,12 +12,16 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"] categories = ["gui"] [features] -# Enables the Image widget +# Enables the `Image` widget image = ["iced_wgpu/image"] -# Enables the Svg widget +# Enables the `Svg` widget svg = ["iced_wgpu/svg"] # Enables a debug view in native platforms (press F12) debug = ["iced_winit/debug"] +# Enables `tokio` as the `executor::Default` on native platforms +tokio = ["iced_futures/tokio"] +# Enables `async-std` as the `executor::Default` on native platforms +async-std = ["iced_futures/async-std"] [badges] maintenance = { status = "actively-developed" } @@ -45,6 +49,9 @@ members = [ "examples/tour", ] +[dependencies] +iced_futures = { version = "0.1.0-alpha", path = "futures" } + [target.'cfg(not(target_arch = "wasm32"))'.dependencies] iced_winit = { version = "0.1.0-alpha", path = "winit" } iced_wgpu = { version = "0.1.0", path = "wgpu" } diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index f8668be0..c1e3edb5 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -6,8 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["image", "debug"] } -iced_futures = { path = "../../futures", features = ["tokio"] } +iced = { path = "../..", features = ["image", "debug", "tokio"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" rand = { version = "0.7", features = ["wasm-bindgen"] } diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 13e420a9..b0cb3c55 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -27,12 +27,7 @@ enum Message { } impl Application for Pokedex { - #[cfg(not(target_arch = "wasm32"))] - type Executor = iced_futures::executor::Tokio; - - #[cfg(target_arch = "wasm32")] - type Executor = iced_futures::executor::WasmBindgen; - + type Executor = iced::executor::Default; type Message = Message; fn new() -> (Pokedex, Command<Message>) { diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index cfb8e97d..c905fc38 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -6,8 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../.." } -iced_futures = { path = "../../futures", features = ["async-std"] } +iced = { path = "../..", features = ["async-std"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 8262b537..a4009874 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -38,12 +38,7 @@ enum Message { } impl Application for Todos { - #[cfg(not(target_arch = "wasm32"))] - type Executor = iced_futures::executor::AsyncStd; - - #[cfg(target_arch = "wasm32")] - type Executor = iced_futures::executor::WasmBindgen; - + type Executor = iced::executor::Default; type Message = Message; fn new() -> (Todos, Command<Message>) { diff --git a/src/executor.rs b/src/executor.rs index 539bfd5d..e31bd93d 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -5,38 +5,49 @@ pub use platform::Default; #[cfg(not(target_arch = "wasm32"))] mod platform { - use iced_winit::{executor::ThreadPool, futures, Executor}; + use iced_futures::{executor, futures}; + + #[cfg(feature = "tokio")] + type Executor = executor::Tokio; + + #[cfg(all(not(feature = "tokio"), feature = "async-std"))] + type Executor = executor::AsyncStd; + + #[cfg(not(any(feature = "tokio", feature = "async-std")))] + type Executor = executor::ThreadPool; /// A default cross-platform executor. /// - /// - On native platforms, it will use a - /// `iced_futures::executor::ThreadPool`. + /// - On native platforms, it will use: + /// - `iced_futures::executor::Tokio` when the `tokio` feature is enabled. + /// - `iced_futures::executor::AsyncStd` when the `async-std` feature is + /// enabled. + /// - `iced_futures::executor::ThreadPool` otherwise. /// - On the Web, it will use `iced_futures::executor::WasmBindgen`. #[derive(Debug)] - pub struct Default(ThreadPool); + pub struct Default(Executor); - impl Executor for Default { + impl super::Executor for Default { fn new() -> Result<Self, futures::io::Error> { - Ok(Default(ThreadPool::new()?)) + Ok(Default(Executor::new()?)) } fn spawn( &self, future: impl futures::Future<Output = ()> + Send + 'static, ) { - self.0.spawn(future); + let _ = self.0.spawn(future); } } } #[cfg(target_arch = "wasm32")] mod platform { - use iced_web::{executor::WasmBindgen, futures, Executor}; + use iced_futures::{executor::WasmBindgen, futures, Executor}; /// A default cross-platform executor. /// - /// - On native platforms, it will use a - /// `iced_futures::executor::ThreadPool`. + /// - On native platforms, it will use `iced_futures::executor::ThreadPool`. /// - On the Web, it will use `iced_futures::executor::WasmBindgen`. #[derive(Debug)] pub struct Default(WasmBindgen); |