diff options
author | 2020-01-20 05:43:09 +0100 | |
---|---|---|
committer | 2020-01-20 05:43:09 +0100 | |
commit | 04086a90c9e933ebfb42de378054e1115b33529d (patch) | |
tree | f0a9e25216807f9a20d757eadf420456d16f613f /src/executor.rs | |
parent | 90690702e1e4abab804ec91e8ff4183824bec436 (diff) | |
download | iced-04086a90c9e933ebfb42de378054e1115b33529d.tar.gz iced-04086a90c9e933ebfb42de378054e1115b33529d.tar.bz2 iced-04086a90c9e933ebfb42de378054e1115b33529d.zip |
Implement `WasmBindgen` executor and reorganize
Diffstat (limited to 'src/executor.rs')
-rw-r--r-- | src/executor.rs | 54 |
1 files changed, 54 insertions, 0 deletions
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); + } + } +} |