stream: work around the rustc bug in StreamExt::collect (#7754)

This commit is contained in:
Tethys Svensson
2025-12-05 10:24:29 +08:00
committed by GitHub
parent 5471a5835e
commit c8116ecd7b
2 changed files with 9 additions and 10 deletions
+1 -1
View File
@@ -916,7 +916,7 @@ pub trait StreamExt: Stream {
/// assert_eq!(Err("no"), values); /// assert_eq!(Err("no"), values);
/// # } /// # }
/// ``` /// ```
fn collect<T>(self) -> Collect<Self, T> fn collect<T>(self) -> Collect<Self, T, T::InternalCollection>
where where
T: FromStream<Self::Item>, T: FromStream<Self::Item>,
Self: Sized, Self: Sized,
+8 -9
View File
@@ -1,7 +1,7 @@
use crate::Stream; use crate::Stream;
use core::future::Future; use core::future::Future;
use core::marker::PhantomPinned; use core::marker::{PhantomData, PhantomPinned};
use core::mem; use core::mem;
use core::pin::Pin; use core::pin::Pin;
use core::task::{ready, Context, Poll}; use core::task::{ready, Context, Poll};
@@ -12,14 +12,12 @@ pin_project! {
/// Future returned by the [`collect`](super::StreamExt::collect) method. /// Future returned by the [`collect`](super::StreamExt::collect) method.
#[must_use = "futures do nothing unless you `.await` or poll them"] #[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)] #[derive(Debug)]
pub struct Collect<T, U> pub struct Collect<T, U, C>
where
T: Stream,
U: FromStream<T::Item>,
{ {
#[pin] #[pin]
stream: T, stream: T,
collection: U::InternalCollection, collection: C,
_output: PhantomData<U>,
// Make this future `!Unpin` for compatibility with async trait methods. // Make this future `!Unpin` for compatibility with async trait methods.
#[pin] #[pin]
_pin: PhantomPinned, _pin: PhantomPinned,
@@ -38,24 +36,25 @@ pin_project! {
/// enhancements to the Rust language. /// enhancements to the Rust language.
pub trait FromStream<T>: sealed::FromStreamPriv<T> {} pub trait FromStream<T>: sealed::FromStreamPriv<T> {}
impl<T, U> Collect<T, U> impl<T, U> Collect<T, U, U::InternalCollection>
where where
T: Stream, T: Stream,
U: FromStream<T::Item>, U: FromStream<T::Item>,
{ {
pub(super) fn new(stream: T) -> Collect<T, U> { pub(super) fn new(stream: T) -> Collect<T, U, U::InternalCollection> {
let (lower, upper) = stream.size_hint(); let (lower, upper) = stream.size_hint();
let collection = U::initialize(sealed::Internal, lower, upper); let collection = U::initialize(sealed::Internal, lower, upper);
Collect { Collect {
stream, stream,
collection, collection,
_output: PhantomData,
_pin: PhantomPinned, _pin: PhantomPinned,
} }
} }
} }
impl<T, U> Future for Collect<T, U> impl<T, U> Future for Collect<T, U, U::InternalCollection>
where where
T: Stream, T: Stream,
U: FromStream<T::Item>, U: FromStream<T::Item>,