From ce2ed35a1aa2e74ce5417926cc7e56415af36df7 Mon Sep 17 00:00:00 2001 From: Olivier Pinon Date: Sat, 25 Apr 2020 02:03:17 +0200 Subject: #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 --- futures/src/command.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'futures/src/command.rs') 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() -- cgit