summaryrefslogtreecommitdiffstats
path: root/futures/src/stream.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-07-05 02:15:13 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-07-05 02:16:45 +0200
commit8bc49cd88653309f5abe8a38d5a4af36fcfea933 (patch)
tree6c205ff9964cec1d48934b7ff166532491a349c5 /futures/src/stream.rs
parente50aa03edc858d561992d8ca441aa063f273eeac (diff)
downloadiced-8bc49cd88653309f5abe8a38d5a4af36fcfea933.tar.gz
iced-8bc49cd88653309f5abe8a38d5a4af36fcfea933.tar.bz2
iced-8bc49cd88653309f5abe8a38d5a4af36fcfea933.zip
Hide `Subscription` internals
.. and introduce `stream::channel` helper
Diffstat (limited to 'futures/src/stream.rs')
-rw-r--r--futures/src/stream.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/futures/src/stream.rs b/futures/src/stream.rs
new file mode 100644
index 00000000..2ec505f1
--- /dev/null
+++ b/futures/src/stream.rs
@@ -0,0 +1,26 @@
+//! Create asynchronous streams of data.
+use futures::channel::mpsc;
+use futures::never::Never;
+use futures::stream::{self, Stream, StreamExt};
+
+use std::future::Future;
+
+/// Creates a new [`Stream`] that produces the items sent from a [`Future`]
+/// to the [`mpsc::Sender`] provided to the closure.
+///
+/// This is a more ergonomic [`stream::unfold`], which allows you to go
+/// from the "world of futures" to the "world of streams" by simply looping
+/// and publishing to an async channel from inside a [`Future`].
+pub fn channel<T, F>(
+ size: usize,
+ f: impl FnOnce(mpsc::Sender<T>) -> F,
+) -> impl Stream<Item = T>
+where
+ F: Future<Output = Never>,
+{
+ let (sender, receiver) = mpsc::channel(size);
+
+ let runner = stream::once(f(sender)).map(|_| unreachable!());
+
+ stream::select(receiver, runner)
+}