diff options
author | 2020-04-29 07:34:14 +0200 | |
---|---|---|
committer | 2020-04-29 07:34:14 +0200 | |
commit | 61c707fe044e7abc035f1a35697757f55f955417 (patch) | |
tree | d5c99fe78400bb51b2d97ebb9145e897e5f4473a /futures | |
parent | dc51080328caa12d2b1fc02febc72cab70bb9f50 (diff) | |
parent | e0aa89cee764eebb1cb06f0b0653f82b337620ea (diff) | |
download | iced-61c707fe044e7abc035f1a35697757f55f955417.tar.gz iced-61c707fe044e7abc035f1a35697757f55f955417.tar.bz2 iced-61c707fe044e7abc035f1a35697757f55f955417.zip |
Merge branch 'master' into feature/canvas-interaction
Diffstat (limited to 'futures')
-rw-r--r-- | futures/Cargo.toml | 2 | ||||
-rw-r--r-- | futures/src/command.rs | 52 |
2 files changed, 53 insertions, 1 deletions
diff --git a/futures/Cargo.toml b/futures/Cargo.toml index e0815d9d..f0e2a104 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_futures" -version = "0.1.1" +version = "0.1.2" authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] edition = "2018" description = "Commands, subscriptions, and runtimes for Iced" 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<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,9 +37,23 @@ 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 + #[cfg(not(target_arch = "wasm32"))] pub fn map<A>( mut self, f: impl Fn(T) -> A + 'static + Send + Sync, @@ -62,6 +77,30 @@ impl<T> Command<T> { } } + /// Applies a transformation to the result of a [`Command`]. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn map<A>(mut self, f: impl Fn(T) -> A + 'static) -> Command<A> + 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<A> + }) + .collect(), + } + } + /// Creates a [`Command`] that performs the actions of all the given /// commands. /// @@ -85,6 +124,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 +136,18 @@ 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() |