Skip to content
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

Implement FusedStream for a few types #2048

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 6 additions & 0 deletions tokio/src/sync/mpsc/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ impl<T> Receiver<T> {
impl<T> Unpin for Receiver<T> {}

cfg_stream! {
impl<T> futures_core::FusedStream for Receiver<T> {
fn is_terminated(&self) -> bool {
self.chan.is_closed()
}
}

impl<T> crate::stream::Stream for Receiver<T> {
type Item = T;

Expand Down
9 changes: 9 additions & 0 deletions tokio/src/sync/mpsc/chan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ where
}
})
}

/// Test if this receiver side of the channel is closed.
#[cfg(feature = "stream")]
pub(crate) fn is_closed(&self) -> bool {
self.inner
.rx_fields
.with(|rx_fields_ptr| unsafe { (*rx_fields_ptr).rx_closed })
&& self.inner.semaphore.is_idle()
}
}

impl<T, S> Drop for Rx<T, S>
Expand Down
18 changes: 13 additions & 5 deletions tokio/src/time/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,19 @@ impl Interval {
}
}

#[cfg(feature = "stream")]
impl crate::stream::Stream for Interval {
type Item = Instant;
cfg_stream! {
impl futures_core::FusedStream for Interval {
fn is_terminated(&self) -> bool {
// NB: intervals never terminate.
false
}
}

impl crate::stream::Stream for Interval {
type Item = Instant;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Instant>> {
Poll::Ready(Some(ready!(self.poll_tick(cx))))
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Instant>> {
Poll::Ready(Some(ready!(self.poll_tick(cx))))
}
}
}