tokio-stream: implement FusedStream for various stream adaptors (#8096)

`MapWhile` is intentionally excluded because its current implementation
does not track when the closure returns `None` early, making a correct
`is_terminated()` impossible without a separate semantic change.
This commit is contained in:
Rachit2323
2026-05-01 14:09:28 +02:00
committed by GitHub
parent 26dee92b53
commit 5030b3005e
11 changed files with 297 additions and 0 deletions
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::pin::Pin;
use core::task::{ready, Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -48,3 +49,13 @@ where
super::merge_size_hints(self.a.size_hint(), self.b.size_hint())
}
}
impl<T, U> FusedStream for Chain<T, U>
where
T: Stream,
U: FusedStream<Item = T::Item>,
{
fn is_terminated(&self) -> bool {
self.a.is_terminated() && self.b.is_terminated()
}
}
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::fmt;
use core::pin::Pin;
use core::task::{ready, Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -56,3 +57,13 @@ where
(0, self.stream.size_hint().1) // can't know a lower bound, due to the predicate
}
}
impl<St, F> FusedStream for Filter<St, F>
where
St: FusedStream,
F: FnMut(&St::Item) -> bool,
{
fn is_terminated(&self) -> bool {
self.stream.is_terminated()
}
}
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::fmt;
use core::pin::Pin;
use core::task::{ready, Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -56,3 +57,13 @@ where
(0, self.stream.size_hint().1) // can't know a lower bound, due to the predicate
}
}
impl<St, F, T> FusedStream for FilterMap<St, F>
where
St: FusedStream,
F: FnMut(St::Item) -> Option<T>,
{
fn is_terminated(&self) -> bool {
self.stream.is_terminated()
}
}
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::fmt;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -49,3 +50,13 @@ where
self.stream.size_hint()
}
}
impl<St, F, T> FusedStream for Map<St, F>
where
St: FusedStream,
F: FnMut(St::Item) -> T,
{
fn is_terminated(&self) -> bool {
self.stream.is_terminated()
}
}
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -57,6 +58,16 @@ where
}
}
impl<T, U> FusedStream for Merge<T, U>
where
T: Stream,
U: Stream<Item = T::Item>,
{
fn is_terminated(&self) -> bool {
self.a.is_terminated() && self.b.is_terminated()
}
}
fn poll_next<T, U>(
first: Pin<&mut T>,
second: Pin<&mut U>,
+10
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::fmt;
use core::pin::Pin;
use core::task::{ready, Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -61,3 +62,12 @@ where
(lower, upper)
}
}
impl<St> FusedStream for Skip<St>
where
St: FusedStream,
{
fn is_terminated(&self) -> bool {
self.stream.is_terminated()
}
}
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::fmt;
use core::pin::Pin;
use core::task::{ready, Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -71,3 +72,13 @@ where
(lower, upper)
}
}
impl<St, F> FusedStream for SkipWhile<St, F>
where
St: FusedStream,
F: FnMut(&St::Item) -> bool,
{
fn is_terminated(&self) -> bool {
self.stream.is_terminated()
}
}
+10
View File
@@ -4,6 +4,7 @@ use core::cmp;
use core::fmt;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -74,3 +75,12 @@ where
(lower, upper)
}
}
impl<St> FusedStream for Take<St>
where
St: Stream,
{
fn is_terminated(&self) -> bool {
self.remaining == 0
}
}
+11
View File
@@ -3,6 +3,7 @@ use crate::Stream;
use core::fmt;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -77,3 +78,13 @@ where
(0, upper)
}
}
impl<St, F> FusedStream for TakeWhile<St, F>
where
St: Stream,
F: FnMut(&St::Item) -> bool,
{
fn is_terminated(&self) -> bool {
self.done
}
}
+12
View File
@@ -4,6 +4,7 @@ use core::fmt;
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::FusedStream;
use pin_project_lite::pin_project;
pin_project! {
@@ -81,3 +82,14 @@ where
(lower, upper)
}
}
impl<St, F, Fut> FusedStream for Then<St, Fut, F>
where
St: FusedStream,
Fut: Future,
F: FnMut(St::Item) -> Fut,
{
fn is_terminated(&self) -> bool {
self.future.is_none() && self.stream.is_terminated()
}
}