diff --git a/Cargo.toml b/Cargo.toml index 6221ae4b..75499df9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,8 @@ tokio = ["iced_futures/tokio"] tokio_old = ["iced_futures/tokio_old"] # Enables `async-std` as the `executor::Default` on native platforms async-std = ["iced_futures/async-std"] +# Enables `smol` as the `executor::Default` on native platforms +smol = ["iced_futures/smol"] # Enables advanced color conversion via `palette` palette = ["iced_core/palette"] diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 92b504a6..c42cc603 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -36,6 +36,10 @@ version = "1.0" optional = true features = ["unstable"] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.smol] +version = "1.0" +optional = true + [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen-futures = "0.4" diff --git a/futures/src/executor.rs b/futures/src/executor.rs index fa87216a..b35b5bc1 100644 --- a/futures/src/executor.rs +++ b/futures/src/executor.rs @@ -13,6 +13,9 @@ mod tokio_old; #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] mod async_std; +#[cfg(all(not(target_arch = "wasm32"), feature = "smol"))] +mod smol; + #[cfg(target_arch = "wasm32")] mod wasm_bindgen; @@ -30,6 +33,9 @@ pub use self::tokio_old::TokioOld; #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] pub use self::async_std::AsyncStd; +#[cfg(all(not(target_arch = "wasm32"), feature = "smol"))] +pub use self::smol::Smol; + #[cfg(target_arch = "wasm32")] pub use wasm_bindgen::WasmBindgen; diff --git a/futures/src/executor/smol.rs b/futures/src/executor/smol.rs new file mode 100644 index 00000000..deafd43a --- /dev/null +++ b/futures/src/executor/smol.rs @@ -0,0 +1,18 @@ +use crate::Executor; + +use futures::Future; + +/// A `smol` runtime. +#[cfg_attr(docsrs, doc(cfg(feature = "smol")))] +#[derive(Debug)] +pub struct Smol; + +impl Executor for Smol { + fn new() -> Result { + Ok(Self) + } + + fn spawn(&self, future: impl Future + Send + 'static) { + smol::spawn(future).detach(); + } +} diff --git a/futures/src/lib.rs b/futures/src/lib.rs index c7c6fd3a..01cf5c89 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -17,10 +17,22 @@ pub mod executor; pub mod subscription; #[cfg(all( - any(feature = "tokio", feature = "tokio_old", feature = "async-std"), + any( + feature = "tokio", + feature = "tokio_old", + feature = "async-std", + feature = "smol" + ), not(target_arch = "wasm32") ))] -#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio", feature = "async-std"))))] +#[cfg_attr( + docsrs, + doc(cfg(any( + feature = "tokio", + feature = "async-std", + feature = "smol" + ))) +)] pub mod time; pub use command::Command; diff --git a/futures/src/time.rs b/futures/src/time.rs index d015d2f0..86b4a4e7 100644 --- a/futures/src/time.rs +++ b/futures/src/time.rs @@ -13,6 +13,33 @@ pub fn every( struct Every(std::time::Duration); +#[cfg(all( + not(any(feature = "tokio_old", feature = "tokio", feature = "async-std")), + feature = "smol" +))] +impl subscription::Recipe for Every +where + H: std::hash::Hasher, +{ + type Output = std::time::Instant; + + fn hash(&self, state: &mut H) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + self.0.hash(state); + } + + fn stream( + self: Box, + _input: futures::stream::BoxStream<'static, E>, + ) -> futures::stream::BoxStream<'static, Self::Output> { + use futures::stream::StreamExt; + + smol::Timer::interval(self.0).boxed() + } +} + #[cfg(feature = "async-std")] impl subscription::Recipe for Every where @@ -41,7 +68,7 @@ where #[cfg(all( any(feature = "tokio", feature = "tokio_old"), - not(feature = "async-std") + not(any(feature = "async-std", feature = "smol")) ))] impl subscription::Recipe for Every where diff --git a/src/executor.rs b/src/executor.rs index 0333bc1d..2c6d9ec0 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -7,22 +7,40 @@ pub use platform::Default; mod platform { use iced_futures::{executor, futures}; - #[cfg(feature = "tokio_old")] + #[cfg(all( + not(any(feature = "tokio", feature = "smol", feature = "async-std")), + feature = "tokio_old" + ))] type Executor = executor::TokioOld; - #[cfg(all(not(feature = "tokio_old"), feature = "tokio"))] + #[cfg(all( + not(any( + feature = "tokio_old", + feature = "smol", + feature = "async-std" + )), + feature = "tokio" + ))] type Executor = executor::Tokio; - #[cfg(all( - not(any(feature = "tokio_old", feature = "tokio")), - feature = "async-std" - ))] + #[cfg(feature = "async-std")] type Executor = executor::AsyncStd; + #[cfg(all( + not(any( + feature = "tokio_old", + feature = "tokio", + feature = "async-std" + )), + feature = "smol" + ))] + type Executor = executor::Smol; + #[cfg(not(any( feature = "tokio_old", feature = "tokio", - feature = "async-std" + feature = "async-std", + feature = "smol", )))] type Executor = executor::ThreadPool; diff --git a/src/lib.rs b/src/lib.rs index 3578ea82..dedcac7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,7 +191,12 @@ pub mod widget; pub mod window; #[cfg(all( - any(feature = "tokio", feature = "tokio_old", feature = "async-std"), + any( + feature = "tokio", + feature = "tokio_old", + feature = "async-std", + feature = "smol" + ), not(target_arch = "wasm32") ))] #[cfg_attr( @@ -200,6 +205,7 @@ pub mod window; feature = "tokio", feature = "tokio_old", feature = "async-std" + feature = "smol" ))) )] pub mod time;