diff options
author | 2023-11-29 14:26:44 +0100 | |
---|---|---|
committer | 2023-11-29 14:26:44 +0100 | |
commit | 7f8b17604a31e00becc43130ec516c1a53552c88 (patch) | |
tree | b7cbc5ba003d61416d7959a6a704839b53822660 /runtime/src/command.rs | |
parent | 7e7d65a23d864e4662c43028a41244ca30cac540 (diff) | |
parent | a761448858521d11dc646e2ef5217e9e06628932 (diff) | |
download | iced-7f8b17604a31e00becc43130ec516c1a53552c88.tar.gz iced-7f8b17604a31e00becc43130ec516c1a53552c88.tar.bz2 iced-7f8b17604a31e00becc43130ec516c1a53552c88.zip |
Merge pull request #2150 from iced-rs/feature/command-run
`Stream` support for `Command`
Diffstat (limited to 'runtime/src/command.rs')
-rw-r--r-- | runtime/src/command.rs | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/runtime/src/command.rs b/runtime/src/command.rs index b74097bd..f70da915 100644 --- a/runtime/src/command.rs +++ b/runtime/src/command.rs @@ -4,8 +4,11 @@ mod action; pub use action::Action; use crate::core::widget; +use crate::futures::futures; use crate::futures::MaybeSend; +use futures::channel::mpsc; +use futures::Stream; use std::fmt; use std::future::Future; @@ -43,11 +46,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. /// @@ -106,3 +119,23 @@ impl<T> fmt::Debug for Command<T> { command.fmt(f) } } + +/// Creates a [`Command`] that produces the `Message`s published from a [`Future`] +/// to an [`mpsc::Sender`] with the given bounds. +pub fn channel<Fut, Message>( + size: usize, + f: impl FnOnce(mpsc::Sender<Message>) -> Fut + MaybeSend + 'static, +) -> Command<Message> +where + Fut: Future<Output = ()> + MaybeSend + 'static, + Message: 'static + MaybeSend, +{ + use futures::future; + use futures::stream::{self, StreamExt}; + + let (sender, receiver) = mpsc::channel(size); + + let runner = stream::once(f(sender)).filter_map(|_| future::ready(None)); + + Command::single(Action::Stream(Box::pin(stream::select(receiver, runner)))) +} |