Merge pull request #699 from JayceFayne/smol

Add `smol` async runtime support
This commit is contained in:
Héctor Ramón 2021-01-15 19:04:23 +01:00 committed by GitHub
commit 2056304e39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 151 additions and 11 deletions

View File

@ -6,9 +6,54 @@ 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]
[#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/
[#370]: https://github.com/hecrj/iced/pull/370
## [0.1.1] - 2020-04-15
### Added
@ -114,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

View File

@ -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"]

View File

@ -6,4 +6,4 @@ edition = "2018"
publish = false
[dependencies]
iced = { path = "../..", features = ["tokio"] }
iced = { path = "../..", features = ["smol"] }

View File

@ -36,6 +36,10 @@ version = "1.0"
optional = true
features = ["unstable"]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.smol]
version = "1.2"
optional = true
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"

View File

@ -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;

View File

@ -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<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
smol::spawn(future).detach();
}
}

View File

@ -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;

View File

@ -13,6 +13,41 @@ pub fn every<H: std::hash::Hasher, E>(
struct Every(std::time::Duration);
#[cfg(all(
not(any(feature = "tokio_old", feature = "tokio", feature = "async-std")),
feature = "smol"
))]
impl<H, E> subscription::Recipe<H, E> 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::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
use std::time::Instant;
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()
}
}
#[cfg(feature = "async-std")]
impl<H, E> subscription::Recipe<H, E> for Every
where
@ -41,7 +76,7 @@ where
#[cfg(all(
any(feature = "tokio", feature = "tokio_old"),
not(feature = "async-std")
not(any(feature = "async-std", feature = "smol"))
))]
impl<H, E> subscription::Recipe<H, E> for Every
where

View File

@ -10,19 +10,30 @@ mod platform {
#[cfg(feature = "tokio_old")]
type Executor = executor::TokioOld;
#[cfg(all(not(feature = "tokio_old"), feature = "tokio"))]
#[cfg(all(feature = "tokio", not(feature = "tokio_old")))]
type Executor = executor::Tokio;
#[cfg(all(
feature = "async-std",
not(any(feature = "tokio_old", feature = "tokio")),
feature = "async-std"
))]
type Executor = executor::AsyncStd;
#[cfg(all(
feature = "smol",
not(any(
feature = "tokio_old",
feature = "tokio",
feature = "async-std"
)),
))]
type Executor = executor::Smol;
#[cfg(not(any(
feature = "tokio_old",
feature = "tokio",
feature = "async-std"
feature = "async-std",
feature = "smol",
)))]
type Executor = executor::ThreadPool;

View File

@ -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;