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
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 Cycle<S> {
template: S,
#[pin]
stream: S,
}
}
impl<S: Clone> Cycle<S> {
pub(crate) fn new(stream: S) -> Self {
Self {
template: stream.clone(),
stream,
}
}
}
impl<S: Clone> CompletionStream for Cycle<S>
where
S: CompletionStream,
{
type Item = S::Item;
unsafe fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
if let Some(val) = ready!(this.stream.as_mut().poll_next(cx)) {
Poll::Ready(Some(val))
} else {
this.stream.as_mut().set(this.template.clone());
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>) {
match self.template.size_hint() {
(0, Some(0)) => (0, Some(0)),
(0, _) => (0, None),
_ => (usize::MAX, None),
}
}
}
impl<S: Clone> Stream for Cycle<S>
where
S: CompletionStream + Stream<Item = <Self as CompletionStream>::Item>,
{
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)
}
}