summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-01-21 00:15:01 +0100
committerLibravatar GitHub <noreply@github.com>2020-01-21 00:15:01 +0100
commit7016221556ea8183ebcd8ef8df00044e2eda71e7 (patch)
treebc1609b71b88437fc7497af339b6427f63121c76 /src
parent6ca5e6184f9f1c12b427bdafcce0b4e9fbc5bb14 (diff)
parent91d9d65a03ce9b211e4043726e7424949d314325 (diff)
downloadiced-7016221556ea8183ebcd8ef8df00044e2eda71e7.tar.gz
iced-7016221556ea8183ebcd8ef8df00044e2eda71e7.tar.bz2
iced-7016221556ea8183ebcd8ef8df00044e2eda71e7.zip
Merge pull request #164 from hecrj/feature/custom-runtime
Custom futures executor with `iced_futures`
Diffstat (limited to '')
-rw-r--r--src/application.rs14
-rw-r--r--src/element.rs9
-rw-r--r--src/executor.rs54
-rw-r--r--src/lib.rs26
-rw-r--r--src/sandbox.rs3
-rw-r--r--src/web.rs1
-rw-r--r--src/widget.rs (renamed from src/native.rs)56
7 files changed, 121 insertions, 42 deletions
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<Message>) {
@@ -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<A::Message>) {
diff --git a/src/element.rs b/src/element.rs
new file mode 100644
index 00000000..e5356fb6
--- /dev/null
+++ b/src/element.rs
@@ -0,0 +1,9 @@
+/// A generic widget.
+///
+/// This is an alias of an `iced_native` element with a default `Renderer`.
+#[cfg(not(target_arch = "wasm32"))]
+pub type Element<'a, Message> =
+ iced_winit::Element<'a, Message, iced_wgpu::Renderer>;
+
+#[cfg(target_arch = "wasm32")]
+pub use iced_web::Element;
diff --git a/src/executor.rs b/src/executor.rs
new file mode 100644
index 00000000..cbbd8283
--- /dev/null
+++ b/src/executor.rs
@@ -0,0 +1,54 @@
+//! Choose your preferred executor to power your application.
+pub use crate::common::{executor::Null, Executor};
+
+pub use platform::Default;
+
+#[cfg(not(target_arch = "wasm32"))]
+mod platform {
+ use iced_winit::{executor::ThreadPool, futures, Executor};
+
+ /// A default cross-platform executor.
+ ///
+ /// - On native platforms, it will use a `iced_futures::executor::ThreadPool`.
+ /// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
+ #[derive(Debug)]
+ pub struct Default(ThreadPool);
+
+ impl Executor for Default {
+ fn new() -> Result<Self, futures::io::Error> {
+ Ok(Default(ThreadPool::new()?))
+ }
+
+ fn spawn(
+ &self,
+ future: impl futures::Future<Output = ()> + Send + 'static,
+ ) {
+ self.0.spawn(future);
+ }
+ }
+}
+
+#[cfg(target_arch = "wasm32")]
+mod platform {
+ use iced_web::{executor::WasmBindgen, futures, Executor};
+
+ /// A default cross-platform executor.
+ ///
+ /// - On native platforms, it will use a `iced_futures::executor::ThreadPool`.
+ /// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
+ #[derive(Debug)]
+ pub struct Default(WasmBindgen);
+
+ impl Executor for Default {
+ fn new() -> Result<Self, futures::io::Error> {
+ Ok(Default(WasmBindgen::new()?))
+ }
+
+ fn spawn(
+ &self,
+ future: impl futures::Future<Output = ()> + Send + 'static,
+ ) {
+ self.0.spawn(future);
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 759dea2f..1da3f549 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -180,18 +180,30 @@
#![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 element;
mod sandbox;
+pub mod executor;
pub mod settings;
+pub mod widget;
pub mod window;
+#[doc(no_inline)]
+pub use widget::*;
+
pub use application::Application;
-pub use platform::*;
+pub use element::Element;
+pub use executor::Executor;
pub use sandbox::Sandbox;
pub use settings::Settings;
+
+#[cfg(not(target_arch = "wasm32"))]
+use iced_winit as common;
+
+#[cfg(target_arch = "wasm32")]
+use iced_web as common;
+
+pub use common::{
+ futures, Align, Background, Color, Command, Font, HorizontalAlignment,
+ Length, Space, Subscription, Vector, VerticalAlignment,
+};
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<T> Application for T
where
T: Sandbox,
{
+ type Executor = executor::Null;
type Message = T::Message;
fn new() -> (Self, Command<T::Message>) {
diff --git a/src/web.rs b/src/web.rs
deleted file mode 100644
index 31f1a6fc..00000000
--- a/src/web.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub use iced_web::*;
diff --git a/src/native.rs b/src/widget.rs
index 35441a3e..7d3a1cef 100644
--- a/src/native.rs
+++ b/src/widget.rs
@@ -1,27 +1,23 @@
-pub use iced_winit::{
- Align, Background, Color, Command, Font, HorizontalAlignment, Length,
- Space, Subscription, Vector, VerticalAlignment,
-};
-
-pub mod widget {
- //! Display information and interactive controls in your application.
- //!
- //! # Re-exports
- //! For convenience, the contents of this module are available at the root
- //! module. Therefore, you can directly type:
- //!
- //! ```
- //! use iced::{button, Button};
- //! ```
- //!
- //! # Stateful widgets
- //! Some widgets need to keep track of __local state__.
- //!
- //! These widgets have their own module with a `State` type. For instance, a
- //! [`TextInput`] has some [`text_input::State`].
- //!
- //! [`TextInput`]: text_input/struct.TextInput.html
- //! [`text_input::State`]: text_input/struct.State.html
+//! Display information and interactive controls in your application.
+//!
+//! # Re-exports
+//! For convenience, the contents of this module are available at the root
+//! module. Therefore, you can directly type:
+//!
+//! ```
+//! use iced::{button, Button};
+//! ```
+//!
+//! # Stateful widgets
+//! Some widgets need to keep track of __local state__.
+//!
+//! These widgets have their own module with a `State` type. For instance, a
+//! [`TextInput`] has some [`text_input::State`].
+//!
+//! [`TextInput`]: text_input/struct.TextInput.html
+//! [`text_input::State`]: text_input/struct.State.html
+#[cfg(not(target_arch = "wasm32"))]
+mod platform {
pub use iced_wgpu::widget::*;
pub mod image {
@@ -56,11 +52,9 @@ pub mod widget {
iced_winit::Row<'a, Message, iced_wgpu::Renderer>;
}
-#[doc(no_inline)]
-pub use widget::*;
+#[cfg(target_arch = "wasm32")]
+mod platform {
+ pub use iced_web::widget::*;
+}
-/// A generic widget.
-///
-/// This is an alias of an `iced_native` element with a default `Renderer`.
-pub type Element<'a, Message> =
- iced_winit::Element<'a, Message, iced_wgpu::Renderer>;
+pub use platform::*;