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..063e9b68 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,30 @@ 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::rc::Rc::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 +124,7 @@ impl Command { } } +#[cfg(not(target_arch = "wasm32"))] impl From for Command where A: Future + 'static + Send, @@ -96,6 +136,18 @@ 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()