summaryrefslogtreecommitdiffstats
path: root/futures/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-04-25 02:39:10 +0200
committerLibravatar GitHub <noreply@github.com>2020-04-25 02:39:10 +0200
commitf9bfa82ebcc316c7868487f3ccda0cb760a85fac (patch)
tree816dae3ede0aa78d5c53129fcd650469ec022710 /futures/src
parent70081c96496ec58563150fe32ee2c4e2a0d46e6a (diff)
parent63f54edf0c4008bb9e4011959863c09e88f4ca77 (diff)
downloadiced-f9bfa82ebcc316c7868487f3ccda0cb760a85fac.tar.gz
iced-f9bfa82ebcc316c7868487f3ccda0cb760a85fac.tar.bz2
iced-f9bfa82ebcc316c7868487f3ccda0cb760a85fac.zip
Merge pull request #322 from AlisCode/aliscode/321/fix-async-examples
#321 Fix async examples by feature-gating Command implementations + A…
Diffstat (limited to 'futures/src')
-rw-r--r--futures/src/command.rs52
1 files changed, 52 insertions, 0 deletions
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()