Skip to content

Add initial Fuse implementation for Stream #40

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
2 changes: 1 addition & 1 deletion src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use from_stream::FromStream;
pub use into_stream::IntoStream;
pub use once::{once, Once};
pub use repeat::{repeat, Repeat};
pub use stream::{Scan, Stream, Take, Zip};
pub use stream::{Fuse, Scan, Stream, Take, Zip};

mod double_ended_stream;
mod empty;
Expand Down
33 changes: 33 additions & 0 deletions src/stream/stream/fuse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::pin::Pin;
use std::task::{Context, Poll};

/// A `Stream` that is permanently closed once a single call to `poll` results in
/// `Poll::Ready(None)`, returning `Poll::Ready(None)` for all future calls to `poll`.
#[derive(Clone, Debug)]
pub struct Fuse<S> {
pub(crate) stream: S,
pub(crate) done: bool,
}

impl<S: Unpin> Unpin for Fuse<S> {}

impl<S: futures_core::Stream> Fuse<S> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(done: bool);
}

impl<S: futures_core::Stream> futures_core::Stream for Fuse<S> {
type Item = S::Item;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
if self.done {
Poll::Ready(None)
} else {
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
if next.is_none() {
*self.as_mut().done() = true;
}
Poll::Ready(next)
}
}
}
31 changes: 31 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ mod filter_map;
mod find;
mod find_map;
mod fold;
mod fuse;
mod min_by;
mod next;
mod nth;
mod scan;
mod take;
mod zip;

pub use fuse::Fuse;
pub use scan::Scan;
pub use take::Take;
pub use zip::Zip;
Expand Down Expand Up @@ -246,6 +248,35 @@ pub trait Stream {
Enumerate::new(self)
}

/// Transforms this `Stream` into a "fused" `Stream` such that after the first time `poll`
/// returns `Poll::Ready(None)`, all future calls to `poll` will also return
/// `Poll::Ready(None)`.
///
/// # Examples
///
/// ```
/// # fn main() { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::stream;
///
/// let mut s = stream::once(1).fuse();
/// assert_eq!(s.next().await, Some(1));
/// assert_eq!(s.next().await, None);
/// assert_eq!(s.next().await, None);
/// #
/// # }) }
/// ```
fn fuse(self) -> Fuse<Self>
where
Self: Sized,
{
Fuse {
stream: self,
done: false,
}
}

/// Both filters and maps a stream.
///
/// # Examples
Expand Down