-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcount.rs
54 lines (44 loc) · 1.29 KB
/
count.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use async_std::prelude::*;
use async_std::task::{self, Context, Poll};
use std::pin::Pin;
use crate::ParallelStream;
pin_project_lite::pin_project! {
/// Count the number of items of the stream.
///
/// This `struct` is created by the [`count`] method on [`ParallelStream`]. See its
/// documentation for more.
///
/// [`count`]: trait.ParallelStream.html#method.count
/// [`ParallelStream`]: trait.ParallelStream.html
#[derive(Clone, Debug)]
pub struct Count<S> {
#[pin]
stream: S,
count: usize,
}
}
impl<S: ParallelStream> Count<S> {
pub(super) fn new(stream: S) -> Self {
Self { stream, count: 0 }
}
}
impl<S: ParallelStream> Future for Count<S> {
type Output = usize;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match task::ready!(this.stream.poll_next(cx)) {
None => Poll::Ready(*this.count),
Some(_) => {
*this.count += 1;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
}
#[async_std::test]
async fn smoke() {
let s = async_std::stream::repeat(5usize);
let cnt = crate::from_stream(s).take(10).count().await;
assert_eq!(cnt, 10);
}