| 1 | use core::pin::Pin; |
| 2 | use core::future::Future; |
| 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 ForEachFuture<S, F> { |
| 13 | #[pin] |
| 14 | stream: S, |
| 15 | f: F, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl<S, F> ForEachFuture<S, F> { |
| 20 | pub(super) fn new(stream: S, f: F) -> Self { |
| 21 | Self { |
| 22 | stream, |
| 23 | f, |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl<S, F> Future for ForEachFuture<S, F> |
| 29 | where |
| 30 | S: Stream + Sized, |
| 31 | F: FnMut(S::Item), |
| 32 | { |
| 33 | type Output = (); |
| 34 | |
| 35 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 36 | let mut this: Projection<'_, S, F> = self.project(); |
| 37 | loop { |
| 38 | let next: Option<::Item> = futures_core::ready!(this.stream.as_mut().poll_next(cx)); |
| 39 | |
| 40 | match next { |
| 41 | Some(v: ::Item) => (this.f)(v), |
| 42 | None => return Poll::Ready(()), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |