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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use core::cmp;
use core::pin::Pin;
use core::task::{Context, Poll};
use completion_core::CompletionStream;
use futures_core::{ready, Stream};
use pin_project_lite::pin_project;
pin_project! {
#[derive(Debug, Clone)]
pub struct Skip<S> {
#[pin]
stream: S,
to_skip: usize,
}
}
impl<S> Skip<S> {
pub(crate) fn new(stream: S, to_skip: usize) -> Self {
Self { stream, to_skip }
}
}
impl<S: CompletionStream> CompletionStream for Skip<S> {
type Item = S::Item;
unsafe fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
while *this.to_skip > 0 {
if ready!(this.stream.as_mut().poll_next(cx)).is_none() {
return Poll::Ready(None);
}
*this.to_skip -= 1;
}
this.stream.poll_next(cx)
}
unsafe fn poll_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
self.project().stream.poll_cancel(cx)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.stream.size_hint();
(
lower.saturating_sub(self.to_skip),
upper.map(|upper| upper.saturating_sub(self.to_skip)),
)
}
}
impl<S: CompletionStream + Stream<Item = <S as CompletionStream>::Item>> Stream for Skip<S> {
type Item = <Self as CompletionStream>::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unsafe { CompletionStream::poll_next(self, cx) }
}
fn size_hint(&self) -> (usize, Option<usize>) {
CompletionStream::size_hint(self)
}
}
pin_project! {
#[derive(Debug, Clone)]
pub struct Take<S> {
#[pin]
stream: S,
to_take: usize,
}
}
impl<S> Take<S> {
pub(crate) fn new(stream: S, to_take: usize) -> Self {
Self { stream, to_take }
}
}
impl<S: CompletionStream> CompletionStream for Take<S> {
type Item = S::Item;
unsafe fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
Poll::Ready(if *this.to_take == 0 {
None
} else {
let item = ready!(this.stream.poll_next(cx));
*this.to_take -= 1;
item
})
}
unsafe fn poll_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let this = self.project();
if *this.to_take == 0 {
Poll::Ready(())
} else {
this.stream.poll_cancel(cx)
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.to_take == 0 {
(0, Some(0))
} else {
let (lower, upper) = self.stream.size_hint();
(
cmp::min(lower, self.to_take),
Some(upper.map_or(self.to_take, |upper| cmp::min(upper, self.to_take))),
)
}
}
}
impl<S: CompletionStream + Stream<Item = <S as CompletionStream>::Item>> Stream for Take<S> {
type Item = <Self as CompletionStream>::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unsafe { CompletionStream::poll_next(self, cx) }
}
fn size_hint(&self) -> (usize, Option<usize>) {
CompletionStream::size_hint(self)
}
}