summaryrefslogtreecommitdiffstats
path: root/futures/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-07-11 04:18:32 +0200
committerLibravatar GitHub <noreply@github.com>2024-07-11 04:18:32 +0200
commitd8944342376c0a0ca283dafd53496c2bfdf405c7 (patch)
tree3eb240ece3e47133d457b02a26c8ddd24a9c543e /futures/src
parent70f44a6e264f7307531935bf0fb9e33a5ebd81c1 (diff)
parent4ce2a207a6c2f28345dc86804a12f2faab3d07ab (diff)
downloadiced-d8944342376c0a0ca283dafd53496c2bfdf405c7.tar.gz
iced-d8944342376c0a0ca283dafd53496c2bfdf405c7.tar.bz2
iced-d8944342376c0a0ca283dafd53496c2bfdf405c7.zip
Merge pull request #2497 from iced-rs/stream-try-channel
Introduce `stream::try_channel` helper
Diffstat (limited to 'futures/src')
-rw-r--r--futures/src/stream.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/futures/src/stream.rs b/futures/src/stream.rs
index 06f9230d..af2f8c99 100644
--- a/futures/src/stream.rs
+++ b/futures/src/stream.rs
@@ -23,3 +23,24 @@ where
stream::select(receiver, runner)
}
+
+/// Creates a new [`Stream`] that produces the items sent from a [`Future`]
+/// that can fail to the [`mpsc::Sender`] provided to the closure.
+pub fn try_channel<T, E, F>(
+ size: usize,
+ f: impl FnOnce(mpsc::Sender<T>) -> F,
+) -> impl Stream<Item = Result<T, E>>
+where
+ F: Future<Output = Result<(), E>>,
+{
+ let (sender, receiver) = mpsc::channel(size);
+
+ let runner = stream::once(f(sender)).filter_map(|result| async {
+ match result {
+ Ok(()) => None,
+ Err(error) => Some(Err(error)),
+ }
+ });
+
+ stream::select(receiver.map(Ok), runner)
+}