diff options
| author | 2021-01-15 19:04:23 +0100 | |
|---|---|---|
| committer | 2021-01-15 19:04:23 +0100 | |
| commit | 2056304e39c32ef5613eb6e82036586043002c08 (patch) | |
| tree | d6f780a018222ed4a4476cfbc634543c7499b462 /futures | |
| parent | 984583b0cca86d7ba8f865489cbebeb181349c19 (diff) | |
| parent | fd2c96c8e36eb37ea4a53aafe0986b569a4e3753 (diff) | |
| download | iced-2056304e39c32ef5613eb6e82036586043002c08.tar.gz iced-2056304e39c32ef5613eb6e82036586043002c08.tar.bz2 iced-2056304e39c32ef5613eb6e82036586043002c08.zip | |
Merge pull request #699 from JayceFayne/smol
Add `smol` async runtime support
Diffstat (limited to '')
| -rw-r--r-- | futures/Cargo.toml | 4 | ||||
| -rw-r--r-- | futures/src/executor.rs | 6 | ||||
| -rw-r--r-- | futures/src/executor/smol.rs | 18 | ||||
| -rw-r--r-- | futures/src/lib.rs | 16 | ||||
| -rw-r--r-- | futures/src/time.rs | 37 | 
5 files changed, 78 insertions, 3 deletions
| diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 92b504a6..c266f705 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.2" +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<Self, futures::io::Error> { +        Ok(Self) +    } + +    fn spawn(&self, future: impl Future<Output = ()> + 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..c11942d2 100644 --- a/futures/src/time.rs +++ b/futures/src/time.rs @@ -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 | 
