diff options
Diffstat (limited to 'src/stream/any.rs')
-rw-r--r-- | src/stream/any.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/stream/any.rs b/src/stream/any.rs index f2ecad5..aac0ec7 100644 --- a/src/stream/any.rs +++ b/src/stream/any.rs @@ -1,25 +1,34 @@ use crate::stream::Stream; use core::future::Future; +use core::marker::PhantomPinned; use core::pin::Pin; use core::task::{Context, Poll}; +use pin_project_lite::pin_project; -/// Future for the [`any`](super::StreamExt::any) method. -#[derive(Debug)] -#[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct AnyFuture<'a, St: ?Sized, F> { - stream: &'a mut St, - f: F, +pin_project! { + /// Future for the [`any`](super::StreamExt::any) method. + #[derive(Debug)] + #[must_use = "futures do nothing unless you `.await` or poll them"] + pub struct AnyFuture<'a, St: ?Sized, F> { + stream: &'a mut St, + f: F, + // Make this future `!Unpin` for compatibility with async trait methods. + #[pin] + _pin: PhantomPinned, + } } impl<'a, St: ?Sized, F> AnyFuture<'a, St, F> { pub(super) fn new(stream: &'a mut St, f: F) -> Self { - Self { stream, f } + Self { + stream, + f, + _pin: PhantomPinned, + } } } -impl<St: ?Sized + Unpin, F> Unpin for AnyFuture<'_, St, F> {} - impl<St, F> Future for AnyFuture<'_, St, F> where St: ?Sized + Stream + Unpin, @@ -27,12 +36,13 @@ where { type Output = bool; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { - let next = futures_core::ready!(Pin::new(&mut self.stream).poll_next(cx)); + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { + let me = self.project(); + let next = futures_core::ready!(Pin::new(me.stream).poll_next(cx)); match next { Some(v) => { - if (&mut self.f)(v) { + if (me.f)(v) { Poll::Ready(true) } else { cx.waker().wake_by_ref(); |