summaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-09-02 15:50:00 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-09-02 15:50:32 +0700
commit6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027 (patch)
treea34e164af613c679bec7a13059c98ae5a4451387 /native
parent7a335a0408b3ee426949a1936255666bc3383ea9 (diff)
downloadiced-6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027.tar.gz
iced-6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027.tar.bz2
iced-6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027.zip
Hide implementation details of `Command` in `iced_futures`
Diffstat (limited to 'native')
-rw-r--r--native/src/command.rs65
1 files changed, 25 insertions, 40 deletions
diff --git a/native/src/command.rs b/native/src/command.rs
index 16fd73ba..1d41539b 100644
--- a/native/src/command.rs
+++ b/native/src/command.rs
@@ -5,18 +5,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.
@@ -26,7 +27,17 @@ impl<T> Command<T> {
) -> Command<A> {
use iced_futures::futures::FutureExt;
- Command::Single(Action::Future(Box::pin(future.map(f))))
+ Command::single(Action::Future(Box::pin(future.map(f))))
+ }
+
+ /// Creates a [`Command`] that performs the actions of all the given
+ /// commands.
+ ///
+ /// Once this command is run, all the commands will be executed at once.
+ pub fn batch(commands: impl IntoIterator<Item = Command<T>>) -> Self {
+ Self(iced_futures::Command::batch(
+ commands.into_iter().map(|Command(command)| command),
+ ))
}
/// Applies a transformation to the result of a [`Command`].
@@ -37,41 +48,15 @@ impl<T> Command<T> {
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(),
- ),
- }
- }
+ let Command(command) = self;
- /// Creates a [`Command`] that performs the actions of all the given
- /// commands.
- ///
- /// 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();
-
- for command in commands {
- match command {
- Self::None => {}
- Self::Single(command) => batch.push(command),
- Self::Batch(commands) => batch.extend(commands),
- }
- }
-
- 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()
}
}