| 1 | use core::future::Future; |
| 2 | use core::pin::Pin; |
| 3 | |
| 4 | use pin_project_lite::pin_project; |
| 5 | |
| 6 | use crate::stream::Stream; |
| 7 | use crate::task::{Context, Poll}; |
| 8 | |
| 9 | pin_project! { |
| 10 | #[doc (hidden)] |
| 11 | #[allow (missing_debug_implementations)] |
| 12 | pub struct LastFuture<S, T> { |
| 13 | #[pin] |
| 14 | stream: S, |
| 15 | last: Option<T>, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl<S, T> LastFuture<S, T> { |
| 20 | pub(crate) fn new(stream: S) -> Self { |
| 21 | Self { stream, last: None } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl<S> Future for LastFuture<S, S::Item> |
| 26 | where |
| 27 | S: Stream + Unpin + Sized, |
| 28 | S::Item: Copy, |
| 29 | { |
| 30 | type Output = Option<S::Item>; |
| 31 | |
| 32 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 33 | let this: Projection<'_, S, impl Copy> = self.project(); |
| 34 | let next: Option = futures_core::ready!(this.stream.poll_next(cx)); |
| 35 | |
| 36 | match next { |
| 37 | Some(new: impl Copy) => { |
| 38 | cx.waker().wake_by_ref(); |
| 39 | *this.last = Some(new); |
| 40 | Poll::Pending |
| 41 | } |
| 42 | None => Poll::Ready(*this.last), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |