Skip to content

Join stream #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/join_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_core::Stream;

/// A stream joining two or more streams.
///
/// This stream is returned by `join!`.
#[derive(Debug)]
pub struct JoinStream<L, R> {
left: L,
right: R,
}

impl<L, R> Unpin for JoinStream<L, R> {}

impl<L, R> JoinStream<L, R> {
#[doc(hidden)]
pub fn new(left: L, right: R) -> Self {
Self { left, right }
}
}

impl<L, R, T> Stream for JoinStream<L, R>
where
L: Stream<Item = T> + Unpin,
R: Stream<Item = T> + Unpin,
{
type Item = T;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if let Poll::Ready(Some(item)) = Pin::new(&mut self.left).poll_next(cx) {
// The first stream made progress. The JoinStream needs to be polled
// again to check the progress of the second stream.
cx.waker().wake_by_ref();
Poll::Ready(Some(item))
} else {
Pin::new(&mut self.right).poll_next(cx)
}
}
}

/// Combines multiple streams into a single stream of all their outputs.
///
/// This macro is only usable inside of async functions, closures, and blocks.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use async_macros::join_stream as join;
/// use futures::stream::{self, StreamExt};
/// use futures::future::ready;
///
/// let a = stream::once(ready(1u8));
/// let b = stream::once(ready(2u8));
/// let c = stream::once(ready(3u8));
///
/// let mut s = join!(a, b, c);
///
/// assert_eq!(s.next().await, Some(1u8));
/// assert_eq!(s.next().await, Some(2u8));
/// assert_eq!(s.next().await, Some(3u8));
/// assert_eq!(s.next().await, None);
/// # });
/// ```
#[macro_export]
macro_rules! join_stream {
($stream1:ident, $stream2:ident, $($stream:ident),* $(,)?) => {{
let joined = $crate::JoinStream::new($stream1, $stream2);
$(
let joined = $crate::JoinStream::new(joined, $stream);
)*
joined
}};
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! # Examples
//!
//! ```
//! #![feature(async_await)]
//! # futures::executor::block_on(async {
//! use async_macros::join;
//! use futures::future;
Expand All @@ -21,13 +20,15 @@
#![cfg_attr(test, deny(warnings))]

mod join;
mod join_stream;
mod maybe_done;
mod poll_fn;
mod ready;
mod select;
mod try_join;
mod try_select;

pub use join_stream::JoinStream;
pub use maybe_done::MaybeDone;

/// Helper re-exports for use in macros.
Expand Down