diff options
author | 2021-09-02 15:50:00 +0700 | |
---|---|---|
committer | 2021-09-02 15:50:32 +0700 | |
commit | 6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027 (patch) | |
tree | a34e164af613c679bec7a13059c98ae5a4451387 /web | |
parent | 7a335a0408b3ee426949a1936255666bc3383ea9 (diff) | |
download | iced-6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027.tar.gz iced-6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027.tar.bz2 iced-6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027.zip |
Hide implementation details of `Command` in `iced_futures`
Diffstat (limited to 'web')
-rw-r--r-- | web/src/command.rs | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/web/src/command.rs b/web/src/command.rs index ff6965c1..f222795d 100644 --- a/web/src/command.rs +++ b/web/src/command.rs @@ -6,18 +6,19 @@ pub use action::Action; use std::future::Future; /// A set of asynchronous actions to be performed by some runtime. -pub enum Command<T> { - None, - Single(Action<T>), - Batch(Vec<Action<T>>), -} +pub struct Command<T>(iced_futures::Command<Action<T>>); impl<T> Command<T> { /// Creates an empty [`Command`]. /// /// In other words, a [`Command`] that does nothing. - pub fn none() -> Self { - Self::None + pub const fn none() -> Self { + Self(iced_futures::Command::none()) + } + + /// Creates a [`Command`] that performs a single [`Action`]. + pub const fn single(action: Action<T>) -> Self { + Self(iced_futures::Command::single(action)) } /// Creates a [`Command`] that performs the action of the given future. @@ -28,25 +29,7 @@ impl<T> Command<T> { ) -> Command<A> { use iced_futures::futures::FutureExt; - Command::Single(Action::Future(Box::pin(future.map(f)))) - } - - /// Applies a transformation to the result of a [`Command`]. - #[cfg(target_arch = "wasm32")] - pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Clone) -> Command<A> - where - T: 'static, - { - match self { - Self::None => Command::None, - Self::Single(action) => Command::Single(action.map(f)), - Self::Batch(batch) => Command::Batch( - batch - .into_iter() - .map(|action| action.map(f.clone())) - .collect(), - ), - } + Command::single(Action::Future(Box::pin(future.map(f)))) } /// Creates a [`Command`] that performs the actions of all the given @@ -54,24 +37,26 @@ impl<T> Command<T> { /// /// Once this command is run, all the commands will be executed at once. pub fn batch(commands: impl IntoIterator<Item = Command<T>>) -> Self { - let mut batch = Vec::new(); + Self(iced_futures::Command::batch( + commands.into_iter().map(|Command(command)| command), + )) + } - for command in commands { - match command { - Self::None => {} - Self::Single(command) => batch.push(command), - Self::Batch(commands) => batch.extend(commands), - } - } + /// Applies a transformation to the result of a [`Command`]. + #[cfg(target_arch = "wasm32")] + pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Clone) -> Command<A> + where + T: 'static, + { + let Command(command) = self; - Self::Batch(batch) + Command(command.map(move |action| action.map(f.clone()))) } + /// Returns all of the actions of the [`Command`]. pub fn actions(self) -> Vec<Action<T>> { - match self { - Self::None => Vec::new(), - Self::Single(action) => vec![action], - Self::Batch(batch) => batch, - } + let Command(command) = self; + + command.actions() } } |