diff options
author | 2020-04-25 02:03:17 +0200 | |
---|---|---|
committer | 2020-04-25 02:03:17 +0200 | |
commit | ce2ed35a1aa2e74ce5417926cc7e56415af36df7 (patch) | |
tree | c1634f628f41c15fd3ca7fbfd41bb8b929bee27e | |
parent | 70081c96496ec58563150fe32ee2c4e2a0d46e6a (diff) | |
download | iced-ce2ed35a1aa2e74ce5417926cc7e56415af36df7.tar.gz iced-ce2ed35a1aa2e74ce5417926cc7e56415af36df7.tar.bz2 iced-ce2ed35a1aa2e74ce5417926cc7e56415af36df7.zip |
#321 Fix async examples by feature-gating Command implementations + Add pokedex example in CI so that at least one async example is runned on CI
-rw-r--r-- | .github/workflows/test.yml | 2 | ||||
-rw-r--r-- | futures/src/command.rs | 56 |
2 files changed, 58 insertions, 0 deletions
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 520d8da9..9e73d3d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,3 +29,5 @@ jobs: run: cargo check --package iced --target wasm32-unknown-unknown - name: Check compilation of `tour` example run: cargo build --package tour --target wasm32-unknown-unknown + - name: Check compilation of `pokedex` example + run: cargo build --package pokedex --target wasm32-unknown-unknown diff --git a/futures/src/command.rs b/futures/src/command.rs index d4f99b82..a4b3d64b 100644 --- a/futures/src/command.rs +++ b/futures/src/command.rs @@ -27,6 +27,7 @@ impl<T> Command<T> { /// Creates a [`Command`] that performs the action of the given future. /// /// [`Command`]: struct.Command.html + #[cfg(not(target_arch = "wasm32"))] pub fn perform<A>( future: impl Future<Output = T> + 'static + Send, f: impl Fn(T) -> A + 'static + Send, @@ -36,9 +37,23 @@ impl<T> Command<T> { } } + /// Creates a [`Command`] that performs the action of the given future. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn perform<A>( + future: impl Future<Output = T> + 'static, + f: impl Fn(T) -> A + 'static + Send, + ) -> Command<A> { + Command { + futures: vec![Box::pin(future.map(f))], + } + } + /// Applies a transformation to the result of a [`Command`]. /// /// [`Command`]: struct.Command.html + #[cfg(not(target_arch = "wasm32"))] pub fn map<A>( mut self, f: impl Fn(T) -> A + 'static + Send + Sync, @@ -62,6 +77,33 @@ impl<T> Command<T> { } } + /// Applies a transformation to the result of a [`Command`]. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn map<A>( + mut self, + f: impl Fn(T) -> A + 'static, + ) -> Command<A> + where + T: 'static, + { + let f = std::sync::Arc::new(f); + + Command { + futures: self + .futures + .drain(..) + .map(|future| { + let f = f.clone(); + + Box::pin(future.map(move |result| f(result))) + as BoxFuture<A> + }) + .collect(), + } + } + /// Creates a [`Command`] that performs the actions of all the given /// commands. /// @@ -85,6 +127,7 @@ impl<T> Command<T> { } } +#[cfg(not(target_arch = "wasm32"))] impl<T, A> From<A> for Command<T> where A: Future<Output = T> + 'static + Send, @@ -96,6 +139,19 @@ where } } +#[cfg(target_arch = "wasm32")] +impl<T, A> From<A> for Command<T> +where + A: Future<Output = T> + 'static, +{ + fn from(future: A) -> Self { + Self { + futures: vec![future.boxed_local()], + } + } +} + + impl<T> std::fmt::Debug for Command<T> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Command").finish() |