Hide implementation details of Command in iced_futures
This commit is contained in:
parent
7a335a0408
commit
6fce35393f
5 changed files with 125 additions and 84 deletions
|
|
@ -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,7 +29,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(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)
|
||||
let Command(command) = self;
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue