Add `smol` async runtime

This commit is contained in:
Jayce Fayne 2021-01-13 01:48:35 +01:00
parent 92d647d1a6
commit b2415eee61
8 changed files with 104 additions and 11 deletions

View File

@ -41,6 +41,8 @@ tokio = ["iced_futures/tokio"]
tokio_old = ["iced_futures/tokio_old"] tokio_old = ["iced_futures/tokio_old"]
# Enables `async-std` as the `executor::Default` on native platforms # Enables `async-std` as the `executor::Default` on native platforms
async-std = ["iced_futures/async-std"] 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` # Enables advanced color conversion via `palette`
palette = ["iced_core/palette"] palette = ["iced_core/palette"]

View File

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

View File

@ -13,6 +13,9 @@ mod tokio_old;
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
mod async_std; mod async_std;
#[cfg(all(not(target_arch = "wasm32"), feature = "smol"))]
mod smol;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
mod wasm_bindgen; mod wasm_bindgen;
@ -30,6 +33,9 @@ pub use self::tokio_old::TokioOld;
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
pub use self::async_std::AsyncStd; pub use self::async_std::AsyncStd;
#[cfg(all(not(target_arch = "wasm32"), feature = "smol"))]
pub use self::smol::Smol;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub use wasm_bindgen::WasmBindgen; 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; pub mod subscription;
#[cfg(all( #[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") 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 mod time;
pub use command::Command; pub use command::Command;

View File

@ -13,6 +13,33 @@ pub fn every<H: std::hash::Hasher, E>(
struct Every(std::time::Duration); 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")] #[cfg(feature = "async-std")]
impl<H, E> subscription::Recipe<H, E> for Every impl<H, E> subscription::Recipe<H, E> for Every
where where
@ -41,7 +68,7 @@ where
#[cfg(all( #[cfg(all(
any(feature = "tokio", feature = "tokio_old"), 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 impl<H, E> subscription::Recipe<H, E> for Every
where where

View File

@ -7,22 +7,40 @@ pub use platform::Default;
mod platform { mod platform {
use iced_futures::{executor, futures}; 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; 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; type Executor = executor::Tokio;
#[cfg(all( #[cfg(feature = "async-std")]
not(any(feature = "tokio_old", feature = "tokio")),
feature = "async-std"
))]
type Executor = executor::AsyncStd; 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( #[cfg(not(any(
feature = "tokio_old", feature = "tokio_old",
feature = "tokio", feature = "tokio",
feature = "async-std" feature = "async-std",
feature = "smol",
)))] )))]
type Executor = executor::ThreadPool; type Executor = executor::ThreadPool;

View File

@ -191,7 +191,12 @@ pub mod widget;
pub mod window; pub mod window;
#[cfg(all( #[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") not(target_arch = "wasm32")
))] ))]
#[cfg_attr( #[cfg_attr(
@ -200,6 +205,7 @@ pub mod window;
feature = "tokio", feature = "tokio",
feature = "tokio_old", feature = "tokio_old",
feature = "async-std" feature = "async-std"
feature = "smol"
))) )))
)] )]
pub mod time; pub mod time;