summaryrefslogtreecommitdiffstats
path: root/core/src/command.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-11-17 07:02:38 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-11-17 07:02:38 +0100
commite0bdb203f2596a65b6ce8c6fb939f82a6dc24048 (patch)
tree522718443bbf58250b9a0da603ef9382e24410b4 /core/src/command.rs
parent2cbd5d60c46d956fcab685b448a40c1aa5f5b69f (diff)
downloadiced-e0bdb203f2596a65b6ce8c6fb939f82a6dc24048.tar.gz
iced-e0bdb203f2596a65b6ce8c6fb939f82a6dc24048.tar.bz2
iced-e0bdb203f2596a65b6ce8c6fb939f82a6dc24048.zip
Implement future-based `Command` in `iced_core`
Diffstat (limited to 'core/src/command.rs')
-rw-r--r--core/src/command.rs49
1 files changed, 49 insertions, 0 deletions
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<T> {
+ futures: Vec<BoxFuture<'static, T>>,
+}
+
+impl<T> Command<T> {
+ pub fn none() -> Self {
+ Self {
+ futures: Vec::new(),
+ }
+ }
+
+ pub fn attempt<A>(
+ future: impl Future<Output = T> + 'static + Send,
+ f: impl Fn(T) -> A + 'static + Send,
+ ) -> Command<A> {
+ Command {
+ futures: vec![future.map(f).boxed()],
+ }
+ }
+
+ pub fn batch(commands: impl Iterator<Item = Command<T>>) -> Self {
+ Self {
+ futures: commands.flat_map(|command| command.futures).collect(),
+ }
+ }
+
+ pub fn futures(self) -> Vec<BoxFuture<'static, T>> {
+ self.futures
+ }
+}
+
+impl<T, A> From<A> for Command<T>
+where
+ A: Future<Output = T> + 'static + Send,
+{
+ fn from(future: A) -> Self {
+ Self {
+ futures: vec![future.boxed()],
+ }
+ }
+}
+
+impl<T> std::fmt::Debug for Command<T> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("Command").finish()
+ }
+}