diff options
author | 2023-11-29 00:12:48 +0100 | |
---|---|---|
committer | 2023-11-29 00:12:48 +0100 | |
commit | 3b7d479534d9114ed12bb5d9ccd910e85d5c13c7 (patch) | |
tree | ab5c3e2a8f074d9a02dd470bfd659fb1740bf82a /runtime | |
parent | 133f4da9014fcdc331ac44269209ee61ca56d007 (diff) | |
download | iced-3b7d479534d9114ed12bb5d9ccd910e85d5c13c7.tar.gz iced-3b7d479534d9114ed12bb5d9ccd910e85d5c13c7.tar.bz2 iced-3b7d479534d9114ed12bb5d9ccd910e85d5c13c7.zip |
Implement `Command::run` for executing a `Stream` to completion
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/src/command.rs | 14 | ||||
-rw-r--r-- | runtime/src/command/action.rs | 9 |
2 files changed, 21 insertions, 2 deletions
diff --git a/runtime/src/command.rs b/runtime/src/command.rs index b74097bd..b942f3ce 100644 --- a/runtime/src/command.rs +++ b/runtime/src/command.rs @@ -4,8 +4,10 @@ mod action; pub use action::Action; use crate::core::widget; +use crate::futures::futures; use crate::futures::MaybeSend; +use futures::Stream; use std::fmt; use std::future::Future; @@ -43,11 +45,21 @@ impl<T> Command<T> { future: impl Future<Output = A> + 'static + MaybeSend, f: impl FnOnce(A) -> T + 'static + MaybeSend, ) -> Command<T> { - use iced_futures::futures::FutureExt; + use futures::FutureExt; Command::single(Action::Future(Box::pin(future.map(f)))) } + /// Creates a [`Command`] that runs the given stream to completion. + pub fn run<A>( + stream: impl Stream<Item = A> + 'static + MaybeSend, + f: impl Fn(A) -> T + 'static + MaybeSend, + ) -> Command<T> { + use futures::StreamExt; + + Command::single(Action::Stream(Box::pin(stream.map(f)))) + } + /// Creates a [`Command`] that performs the actions of all the given /// commands. /// diff --git a/runtime/src/command/action.rs b/runtime/src/command/action.rs index 6c74f0ef..6551e233 100644 --- a/runtime/src/command/action.rs +++ b/runtime/src/command/action.rs @@ -18,6 +18,11 @@ pub enum Action<T> { /// [`Future`]: iced_futures::BoxFuture Future(iced_futures::BoxFuture<T>), + /// Run a [`Stream`] to completion. + /// + /// [`Stream`]: iced_futures::BoxStream + Stream(iced_futures::BoxStream<T>), + /// Run a clipboard action. Clipboard(clipboard::Action<T>), @@ -52,10 +57,11 @@ impl<T> Action<T> { A: 'static, T: 'static, { - use iced_futures::futures::FutureExt; + use iced_futures::futures::{FutureExt, StreamExt}; match self { Self::Future(future) => Action::Future(Box::pin(future.map(f))), + Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))), Self::Clipboard(action) => Action::Clipboard(action.map(f)), Self::Window(window) => Action::Window(window.map(f)), Self::System(system) => Action::System(system.map(f)), @@ -74,6 +80,7 @@ impl<T> fmt::Debug for Action<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Future(_) => write!(f, "Action::Future"), + Self::Stream(_) => write!(f, "Action::Stream"), Self::Clipboard(action) => { write!(f, "Action::Clipboard({action:?})") } |