Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add smol async runtime support #699

Merged
merged 5 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
2 changes: 1 addition & 1 deletion examples/stopwatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2018"
publish = false

[dependencies]
iced = { path = "../..", features = ["tokio"] }
iced = { path = "../..", features = ["smol"] }
4 changes: 4 additions & 0 deletions futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions futures/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down
18 changes: 18 additions & 0 deletions futures/src/executor/smol.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 14 additions & 2 deletions futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 36 additions & 1 deletion futures/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -200,6 +205,7 @@ pub mod window;
feature = "tokio",
feature = "tokio_old",
feature = "async-std"
feature = "smol"
)))
)]
pub mod time;
Expand Down