You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
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
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 beingSend
.Basically, the
From<A>
implementation forCommand<T>
iniced_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 :
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!
The text was updated successfully, but these errors were encountered: