Merge pull request #322 from AlisCode/aliscode/321/fix-async-examples

#321 Fix async examples by feature-gating Command implementations + A…
This commit is contained in:
Héctor Ramón 2020-04-25 02:39:10 +02:00 committed by GitHub
commit f9bfa82ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -29,3 +29,5 @@ jobs:
run: cargo check --package iced --target wasm32-unknown-unknown run: cargo check --package iced --target wasm32-unknown-unknown
- name: Check compilation of `tour` example - name: Check compilation of `tour` example
run: cargo build --package tour --target wasm32-unknown-unknown run: cargo build --package tour --target wasm32-unknown-unknown
- name: Check compilation of `pokedex` example
run: cargo build --package pokedex --target wasm32-unknown-unknown

View File

@ -27,6 +27,7 @@ impl<T> Command<T> {
/// Creates a [`Command`] that performs the action of the given future. /// Creates a [`Command`] that performs the action of the given future.
/// ///
/// [`Command`]: struct.Command.html /// [`Command`]: struct.Command.html
#[cfg(not(target_arch = "wasm32"))]
pub fn perform<A>( pub fn perform<A>(
future: impl Future<Output = T> + 'static + Send, future: impl Future<Output = T> + 'static + Send,
f: impl Fn(T) -> A + '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`]. /// Applies a transformation to the result of a [`Command`].
/// ///
/// [`Command`]: struct.Command.html /// [`Command`]: struct.Command.html
#[cfg(not(target_arch = "wasm32"))]
pub fn map<A>( pub fn map<A>(
mut self, mut self,
f: impl Fn(T) -> A + 'static + Send + Sync, 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 /// Creates a [`Command`] that performs the actions of all the given
/// commands. /// commands.
/// ///
@ -85,6 +124,7 @@ impl<T> Command<T> {
} }
} }
#[cfg(not(target_arch = "wasm32"))]
impl<T, A> From<A> for Command<T> impl<T, A> From<A> for Command<T>
where where
A: Future<Output = T> + 'static + Send, 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> { impl<T> std::fmt::Debug for Command<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Command").finish() f.debug_struct("Command").finish()