diff options
Diffstat (limited to 'src/stream/try_stream/try_collect.rs')
-rw-r--r-- | src/stream/try_stream/try_collect.rs | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/stream/try_stream/try_collect.rs b/src/stream/try_stream/try_collect.rs index d22e8e8..3c9aee2 100644 --- a/src/stream/try_stream/try_collect.rs +++ b/src/stream/try_stream/try_collect.rs @@ -3,34 +3,27 @@ use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, TryStream}; use futures_core::task::{Context, Poll}; -use pin_utils::{unsafe_pinned, unsafe_unpinned}; +use pin_project::{pin_project, project}; /// Future for the [`try_collect`](super::TryStreamExt::try_collect) method. +#[pin_project] #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct TryCollect<St, C> { + #[pin] stream: St, items: C, } impl<St: TryStream, C: Default> TryCollect<St, C> { - unsafe_pinned!(stream: St); - unsafe_unpinned!(items: C); - pub(super) fn new(s: St) -> TryCollect<St, C> { TryCollect { stream: s, items: Default::default(), } } - - fn finish(self: Pin<&mut Self>) -> C { - mem::replace(self.items(), Default::default()) - } } -impl<St: Unpin + TryStream, C> Unpin for TryCollect<St, C> {} - impl<St, C> FusedFuture for TryCollect<St, C> where St: TryStream + FusedStream, @@ -48,15 +41,18 @@ where { type Output = Result<C, St::Error>; + #[project] fn poll( - mut self: Pin<&mut Self>, + self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Self::Output> { - loop { - match ready!(self.as_mut().stream().try_poll_next(cx)?) { - Some(x) => self.as_mut().items().extend(Some(x)), - None => return Poll::Ready(Ok(self.as_mut().finish())), + #[project] + let TryCollect { mut stream, items } = self.project(); + Poll::Ready(Ok(loop { + match ready!(stream.as_mut().try_poll_next(cx)?) { + Some(x) => items.extend(Some(x)), + None => break mem::replace(items, Default::default()), } - } + })) } } |