Hide Subscription internals
.. and introduce `stream::channel` helper
This commit is contained in:
parent
e50aa03edc
commit
8bc49cd886
11 changed files with 268 additions and 246 deletions
26
futures/src/stream.rs
Normal file
26
futures/src/stream.rs
Normal file
|
|
@ -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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue