Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Examples are broken on wasm32 backend #321

Closed
AlisCode opened this issue Apr 24, 2020 · 0 comments · Fixed by #322
Closed

Async Examples are broken on wasm32 backend #321

AlisCode opened this issue Apr 24, 2020 · 0 comments · Fixed by #322
Labels
bug Something isn't working
Milestone

Comments

@AlisCode
Copy link
Contributor

Hey there,

I tried to compile the Pokedex example with cargo build --target wasm32-unknown-unknown but it failed, saying that there was a problem with the types of my futures not being Send.

Basically, the From<A> implementation for Command<T> in iced_futures/src/command.rs is requiring - even on wasm32 targets - that the A generic (so, the future) be Send. That's fine for the native backend, but the iced-futures wasm-bindgen backend doesn't want its futures to be Send because it's storing them in an Rc<RefCell<..>>, thus making the example crash.

Fiddled around in the source code, managed to get the example to work on wasm32 backend after this diff :

diff --git a/futures/src/command.rs b/futures/src/command.rs
index d4f99b8..60e08b7 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,6 +37,19 @@ 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
@@ -85,6 +99,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 +111,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()

Granted, the implementation is a bit hacky 😄 do you have a better way in mind ? I can make a PR with whatever you feel applicable.

Cheers!

@AlisCode AlisCode changed the title Examples are broken on wasm32 backend Async Examples are broken on wasm32 backend Apr 24, 2020
AlisCode added a commit to AlisCode/iced that referenced this issue Apr 25, 2020
…ons + Add pokedex example in CI so that at least one async example is runned on CI
AlisCode added a commit to AlisCode/iced that referenced this issue Apr 25, 2020
@hecrj hecrj added the bug Something isn't working label Apr 25, 2020
@hecrj hecrj added this to the 0.2.0 milestone Apr 25, 2020
hecrj added a commit that referenced this issue Apr 25, 2020
#321 Fix async examples by feature-gating Command implementations + A…
hecrj added a commit that referenced this issue Apr 26, 2020
#321 Fix async examples by feature-gating Command implementations + A…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants