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 { + 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() + } +} 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; diff --git a/native/Cargo.toml b/native/Cargo.toml index 38db1610..5dc3ae1a 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -8,6 +8,6 @@ license = "MIT" repository = "https://github.com/hecrj/iced" [dependencies] -iced_core = { version = "0.1.0-alpha", path = "../core" } +iced_core = { version = "0.1.0-alpha", path = "../core", features = ["command"] } twox-hash = "1.5" raw-window-handle = "0.3" diff --git a/native/src/lib.rs b/native/src/lib.rs index 7e55064c..bd03ddcd 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -216,7 +216,7 @@ mod size; mod user_interface; pub use iced_core::{ - Align, Background, Color, Font, Length, Point, Rectangle, Vector, + Align, Background, Color, Command, Font, Length, Point, Rectangle, Vector, }; pub use element::Element;