Skip to content

Commit

Permalink
Add smol async runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
JayceFayne committed Jan 14, 2021
1 parent 92d647d commit b2415ee
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 10 deletions.
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
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.0"
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
29 changes: 28 additions & 1 deletion futures/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ 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;

smol::Timer::interval(self.0).boxed()
}
}

#[cfg(feature = "async-std")]
impl<H, E> subscription::Recipe<H, E> for Every
where
Expand Down Expand Up @@ -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<H, E> subscription::Recipe<H, E> for Every
where
Expand Down
30 changes: 24 additions & 6 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(feature = "async-std")]
type Executor = executor::AsyncStd;

#[cfg(all(
not(any(feature = "tokio_old", feature = "tokio")),
feature = "async-std"
not(any(
feature = "tokio_old",
feature = "tokio",
feature = "async-std"
)),
feature = "smol"
))]
type Executor = executor::AsyncStd;
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

0 comments on commit b2415ee

Please sign in to comment.