diff options
| author | 2019-11-17 07:02:38 +0100 | |
|---|---|---|
| committer | 2019-11-17 07:02:38 +0100 | |
| commit | e0bdb203f2596a65b6ce8c6fb939f82a6dc24048 (patch) | |
| tree | 522718443bbf58250b9a0da603ef9382e24410b4 /core | |
| parent | 2cbd5d60c46d956fcab685b448a40c1aa5f5b69f (diff) | |
| download | iced-e0bdb203f2596a65b6ce8c6fb939f82a6dc24048.tar.gz iced-e0bdb203f2596a65b6ce8c6fb939f82a6dc24048.tar.bz2 iced-e0bdb203f2596a65b6ce8c6fb939f82a6dc24048.zip  | |
Implement future-based `Command` in `iced_core`
Diffstat (limited to '')
| -rw-r--r-- | core/Cargo.toml | 7 | ||||
| -rw-r--r-- | core/src/command.rs | 49 | ||||
| -rw-r--r-- | core/src/lib.rs | 4 | 
3 files changed, 60 insertions, 0 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index a244bcba..f2492345 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -6,3 +6,10 @@ edition = "2018"  description = "The essential concepts of Iced"  license = "MIT"  repository = "https://github.com/hecrj/iced" + +[features] +# Exposes a future-based `Command` type +command = ["futures"] + +[dependencies] +futures = { version = "0.3", optional = true } 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() +    } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index b61f2eae..3816f8a2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -3,6 +3,8 @@ pub mod widget;  mod align;  mod background;  mod color; +#[cfg(feature = "command")] +mod command;  mod font;  mod length;  mod point; @@ -12,6 +14,8 @@ mod vector;  pub use align::Align;  pub use background::Background;  pub use color::Color; +#[cfg(feature = "command")] +pub use command::Command;  pub use font::Font;  pub use length::Length;  pub use point::Point;  | 
