From e0bdb203f2596a65b6ce8c6fb939f82a6dc24048 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Nov 2019 07:02:38 +0100 Subject: Implement future-based `Command` in `iced_core` --- core/src/command.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 core/src/command.rs (limited to 'core/src/command.rs') diff --git a/core/src/command.rs b/core/src/command.rs new file mode 100644 index 00000000..ae901bd8 --- /dev/null +++ b/core/src/command.rs @@ -0,0 +1,49 @@ +use futures::future::{BoxFuture, Future, FutureExt}; + +pub struct Command { + futures: Vec>, +} + +impl Command { + pub fn none() -> Self { + Self { + futures: Vec::new(), + } + } + + pub fn attempt( + future: impl Future + 'static + Send, + f: impl Fn(T) -> A + 'static + Send, + ) -> Command { + Command { + futures: vec![future.map(f).boxed()], + } + } + + pub fn batch(commands: impl Iterator>) -> Self { + Self { + futures: commands.flat_map(|command| command.futures).collect(), + } + } + + pub fn futures(self) -> Vec> { + self.futures + } +} + +impl From for Command +where + A: Future + 'static + Send, +{ + fn from(future: A) -> Self { + Self { + futures: vec![future.boxed()], + } + } +} + +impl std::fmt::Debug for Command { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Command").finish() + } +} -- cgit From a803ab240b9b5d1a073454f814229bad9d8bdafc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Nov 2019 00:10:40 +0100 Subject: Rename `Command::attempt` to `Command::perform` --- core/src/command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/command.rs') diff --git a/core/src/command.rs b/core/src/command.rs index ae901bd8..e1c865dd 100644 --- a/core/src/command.rs +++ b/core/src/command.rs @@ -11,7 +11,7 @@ impl Command { } } - pub fn attempt( + pub fn perform( future: impl Future + 'static + Send, f: impl Fn(T) -> A + 'static + Send, ) -> Command { -- cgit