Allow future in stream::channel to return

This commit is contained in:
Héctor Ramón Jiménez 2024-07-08 01:13:22 +02:00
parent 978327f9e7
commit 23d9497e7f
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -1,6 +1,5 @@
//! Create asynchronous streams of data.
use futures::channel::mpsc;
use futures::never::Never;
use futures::stream::{self, Stream, StreamExt};
use std::future::Future;
@ -16,11 +15,11 @@ pub fn channel<T, F>(
f: impl FnOnce(mpsc::Sender<T>) -> F,
) -> impl Stream<Item = T>
where
F: Future<Output = Never>,
F: Future<Output = ()>,
{
let (sender, receiver) = mpsc::channel(size);
let runner = stream::once(f(sender)).map(|_| unreachable!());
let runner = stream::once(f(sender)).filter_map(|_| async { None });
stream::select(receiver, runner)
}