From ce2ed35a1aa2e74ce5417926cc7e56415af36df7 Mon Sep 17 00:00:00 2001 From: Olivier Pinon Date: Sat, 25 Apr 2020 02:03:17 +0200 Subject: [PATCH 1/3] #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 --- .github/workflows/test.yml | 2 ++ futures/src/command.rs | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) 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 Command { /// Creates a [`Command`] that performs the action of the given future. /// /// [`Command`]: struct.Command.html + #[cfg(not(target_arch = "wasm32"))] pub fn perform( future: impl Future + 'static + Send, f: impl Fn(T) -> A + 'static + Send, @@ -36,9 +37,23 @@ impl Command { } } + /// Creates a [`Command`] that performs the action of the given future. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn perform( + future: impl Future + 'static, + f: impl Fn(T) -> A + 'static + Send, + ) -> Command { + 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( mut self, f: impl Fn(T) -> A + 'static + Send + Sync, @@ -62,6 +77,33 @@ impl Command { } } + /// Applies a transformation to the result of a [`Command`]. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn map( + mut self, + f: impl Fn(T) -> A + 'static, + ) -> Command + 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 + }) + .collect(), + } + } + /// Creates a [`Command`] that performs the actions of all the given /// commands. /// @@ -85,6 +127,7 @@ impl Command { } } +#[cfg(not(target_arch = "wasm32"))] impl From for Command where A: Future + 'static + Send, @@ -96,6 +139,19 @@ where } } +#[cfg(target_arch = "wasm32")] +impl From for Command +where + A: Future + 'static, +{ + fn from(future: A) -> Self { + Self { + futures: vec![future.boxed_local()], + } + } +} + + impl std::fmt::Debug for Command { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Command").finish() From f1e18d09354ec8726954581961e3907ab42b521e Mon Sep 17 00:00:00 2001 From: Olivier Pinon Date: Sat, 25 Apr 2020 02:05:17 +0200 Subject: [PATCH 2/3] #321 cargo fmt --- futures/src/command.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/futures/src/command.rs b/futures/src/command.rs index a4b3d64b..25acbda0 100644 --- a/futures/src/command.rs +++ b/futures/src/command.rs @@ -81,10 +81,7 @@ impl Command { /// /// [`Command`]: struct.Command.html #[cfg(target_arch = "wasm32")] - pub fn map( - mut self, - f: impl Fn(T) -> A + 'static, - ) -> Command + pub fn map(mut self, f: impl Fn(T) -> A + 'static) -> Command where T: 'static, { @@ -151,7 +148,6 @@ where } } - impl std::fmt::Debug for Command { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Command").finish() From 63f54edf0c4008bb9e4011959863c09e88f4ca77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 25 Apr 2020 02:29:40 +0200 Subject: [PATCH 3/3] Use `Rc` in `Command::map` for Wasm --- futures/src/command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/futures/src/command.rs b/futures/src/command.rs index 25acbda0..063e9b68 100644 --- a/futures/src/command.rs +++ b/futures/src/command.rs @@ -85,7 +85,7 @@ impl Command { where T: 'static, { - let f = std::sync::Arc::new(f); + let f = std::rc::Rc::new(f); Command { futures: self