commit
c58747b5fc
@ -0,0 +1,44 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
|
use crate::stream::Stream;
|
||||||
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct CountFuture<S> {
|
||||||
|
#[pin]
|
||||||
|
stream: S,
|
||||||
|
count: usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> CountFuture<S> {
|
||||||
|
pub(crate) fn new(stream: S) -> Self {
|
||||||
|
CountFuture { stream, count: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Future for CountFuture<S>
|
||||||
|
where
|
||||||
|
S: Stream,
|
||||||
|
{
|
||||||
|
type Output = usize;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let this = self.project();
|
||||||
|
let next = futures_core::ready!(this.stream.poll_next(cx));
|
||||||
|
|
||||||
|
match next {
|
||||||
|
Some(_) => {
|
||||||
|
cx.waker().wake_by_ref();
|
||||||
|
*this.count += 1;
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
None => Poll::Ready(*this.count),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue