From b2415eee61063d5c2b220d7b7a513d1952ce2be1 Mon Sep 17 00:00:00 2001 From: Jayce Fayne Date: Wed, 13 Jan 2021 01:48:35 +0100 Subject: [PATCH 1/5] Add `smol` async runtime --- Cargo.toml | 2 ++ futures/Cargo.toml | 4 ++++ futures/src/executor.rs | 6 ++++++ futures/src/executor/smol.rs | 18 ++++++++++++++++++ futures/src/lib.rs | 16 ++++++++++++++-- futures/src/time.rs | 29 ++++++++++++++++++++++++++++- src/executor.rs | 32 +++++++++++++++++++++++++------- src/lib.rs | 8 +++++++- 8 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 futures/src/executor/smol.rs 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; From c542224f4b466f9de103d8f67e20ce48583a5461 Mon Sep 17 00:00:00 2001 From: Jayce Fayne Date: Wed, 13 Jan 2021 17:15:47 +0100 Subject: [PATCH 2/5] Update `CHANGELOG.md` --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee650337..e4d1a4fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added - `"system_font"` feature gates reading system fonts. [#370] +- Support for the [`smol`] async runtime. [#699] [#370]: https://github.com/hecrj/iced/pull/370 +[#699]: https://github.com/hecrj/iced/pull/699 +[`smol`]: https://github.com/smol-rs/smol ## [0.1.1] - 2020-04-15 ### Added From a42a0844c2ffc51e117304a5d08e4a0fb52adac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 15 Jan 2021 18:31:30 +0100 Subject: [PATCH 3/5] Keep old behavior for `Executor` feature flags --- src/executor.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 2c6d9ec0..9f3656b1 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -7,32 +7,25 @@ pub use platform::Default; mod platform { use iced_futures::{executor, futures}; - #[cfg(all( - not(any(feature = "tokio", feature = "smol", feature = "async-std")), - feature = "tokio_old" - ))] + #[cfg(feature = "tokio_old")] type Executor = executor::TokioOld; - #[cfg(all( - not(any( - feature = "tokio_old", - feature = "smol", - feature = "async-std" - )), - feature = "tokio" - ))] + #[cfg(all(feature = "tokio", not(feature = "tokio_old")))] type Executor = executor::Tokio; - #[cfg(feature = "async-std")] + #[cfg(all( + feature = "async-std", + not(any(feature = "tokio_old", feature = "tokio")), + ))] type Executor = executor::AsyncStd; #[cfg(all( + feature = "smol", not(any( feature = "tokio_old", feature = "tokio", feature = "async-std" )), - feature = "smol" ))] type Executor = executor::Smol; From bcc54b0831d3794e2105f591db4c325615d35041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 15 Jan 2021 18:37:20 +0100 Subject: [PATCH 4/5] Add latest release (`0.2.0`) to `CHANGELOG` --- CHANGELOG.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d1a4fd..70673f79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,13 +6,55 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- `"system_font"` feature gates reading system fonts. [#370] - Support for the [`smol`] async runtime. [#699] -[#370]: https://github.com/hecrj/iced/pull/370 [#699]: https://github.com/hecrj/iced/pull/699 [`smol`]: https://github.com/smol-rs/smol +## [0.2.0] - 2020-11-26 +- __[`Canvas` interactivity][canvas]__ (#325) + A trait-based approach to react to mouse and keyboard interactions in [the `Canvas` widget][#193]. + +- __[`iced_graphics` subcrate][opengl]__ (#354) + A backend-agnostic graphics subcrate that can be leveraged to build new renderers. + +- __[OpenGL renderer][opengl]__ (#354) + An OpenGL renderer powered by [`iced_graphics`], [`glow`], and [`glutin`]. It is an alternative to the default [`wgpu`] renderer. + +- __[Overlay support][pick_list]__ (#444) + Basic support for superpositioning interactive widgets on top of other widgets. + +- __[Faster event loop][view]__ (#597) + The event loop now takes advantage of the data dependencies in [The Elm Architecture] and leverages the borrow checker to keep the widget tree alive between iterations, avoiding unnecessary rebuilds. + +- __[Event capturing][event]__ (#614) + The runtime now can tell whether a widget has handled an event or not, easing [integration with existing applications]. + +- __[`PickList` widget][pick_list]__ (#444) + A drop-down selector widget built on top of the new overlay support. + +- __[`QRCode` widget][qr_code]__ (#622) + A widget that displays a QR code, powered by [the `qrcode` crate]. + +[canvas]: https://github.com/hecrj/iced/pull/325 +[opengl]: https://github.com/hecrj/iced/pull/354 +[`iced_graphics`]: https://github.com/hecrj/iced/pull/354 +[pane_grid]: https://github.com/hecrj/iced/pull/397 +[pick_list]: https://github.com/hecrj/iced/pull/444 +[error]: https://github.com/hecrj/iced/pull/514 +[view]: https://github.com/hecrj/iced/pull/597 +[event]: https://github.com/hecrj/iced/pull/614 +[color]: https://github.com/hecrj/iced/pull/200 +[qr_code]: https://github.com/hecrj/iced/pull/622 +[#193]: https://github.com/hecrj/iced/pull/193 +[`glutin`]: https://github.com/rust-windowing/glutin +[`wgpu`]: https://github.com/gfx-rs/wgpu-rs +[`glow`]: https://github.com/grovesNL/glow +[the `qrcode` crate]: https://docs.rs/qrcode/0.12.0/qrcode/ +[integration with existing applications]: https://github.com/hecrj/iced/pull/183 +[The Elm Architecture]: https://guide.elm-lang.org/architecture/ + + ## [0.1.1] - 2020-04-15 ### Added - `Settings::with_flags` to easily initialize some default settings with flags. [#266] @@ -117,7 +159,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - First release! :tada: -[Unreleased]: https://github.com/hecrj/iced/compare/0.1.1...HEAD +[Unreleased]: https://github.com/hecrj/iced/compare/0.2.0...HEAD +[0.2.0]: https://github.com/hecrj/iced/compare/0.1.1...0.2.0 [0.1.1]: https://github.com/hecrj/iced/compare/0.1.0...0.1.1 [0.1.0]: https://github.com/hecrj/iced/compare/0.1.0-beta...0.1.0 [0.1.0-beta]: https://github.com/hecrj/iced/compare/0.1.0-alpha...0.1.0-beta From fd2c96c8e36eb37ea4a53aafe0986b569a4e3753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 15 Jan 2021 18:52:12 +0100 Subject: [PATCH 5/5] Fix `time::Every` implementation for `smol` runtime --- examples/stopwatch/Cargo.toml | 2 +- futures/Cargo.toml | 2 +- futures/src/time.rs | 10 +++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/stopwatch/Cargo.toml b/examples/stopwatch/Cargo.toml index 075aa111..9f935951 100644 --- a/examples/stopwatch/Cargo.toml +++ b/examples/stopwatch/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["tokio"] } +iced = { path = "../..", features = ["smol"] } diff --git a/futures/Cargo.toml b/futures/Cargo.toml index c42cc603..c266f705 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -37,7 +37,7 @@ optional = true features = ["unstable"] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.smol] -version = "1.0" +version = "1.2" optional = true [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/futures/src/time.rs b/futures/src/time.rs index 86b4a4e7..c11942d2 100644 --- a/futures/src/time.rs +++ b/futures/src/time.rs @@ -35,8 +35,16 @@ where _input: futures::stream::BoxStream<'static, E>, ) -> futures::stream::BoxStream<'static, Self::Output> { use futures::stream::StreamExt; + use std::time::Instant; - smol::Timer::interval(self.0).boxed() + let duration = self.0; + + futures::stream::unfold(Instant::now(), move |last_tick| async move { + let last_tick = smol::Timer::at(last_tick + duration).await; + + Some((last_tick, last_tick)) + }) + .boxed() } }