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
use std::marker::PhantomData;
use futures::{Stream, Future, IntoFuture, Async};
use futures::stream::BufferUnordered;
struct SwallowErrors<F: Future<Item=(), Error=()>, E>(F, PhantomData<E>);
struct MapSwallowErrors<S: Stream>(S)
where S::Item: IntoFuture<Item=(), Error=()>;
pub struct Listen<S: Stream>
where S::Item: IntoFuture<Item=(), Error=()>,
{
buffer: BufferUnordered<MapSwallowErrors<S>>,
}
pub fn new<S: Stream>(stream: S, limit: usize) -> Listen<S>
where S::Item: IntoFuture<Item=(), Error=()>,
{
Listen {
buffer: MapSwallowErrors(stream).buffer_unordered(limit),
}
}
impl<S: Stream> Future for Listen<S>
where S::Item: IntoFuture<Item=(), Error=()>,
{
type Item = ();
type Error = S::Error;
fn poll(&mut self) -> Result<Async<()>, S::Error> {
loop {
match self.buffer.poll()? {
Async::Ready(Some(())) => continue,
Async::NotReady => return Ok(Async::NotReady),
Async::Ready(None) => return Ok(Async::Ready(())),
}
}
}
}
impl<F: Future<Item=(), Error=()>, E> Future for SwallowErrors<F, E> {
type Item = ();
type Error = E;
#[inline(always)]
fn poll(&mut self) -> Result<Async<F::Item>, E> {
match self.0.poll() {
Ok(x) => Ok(x),
Err(()) => Ok(Async::Ready(())),
}
}
}
impl<S: Stream> Stream for MapSwallowErrors<S>
where S::Item: IntoFuture<Item=(), Error=()>,
{
type Item = SwallowErrors<<S::Item as IntoFuture>::Future, S::Error>;
type Error = S::Error;
#[inline(always)]
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, S::Error> {
match self.0.poll()? {
Async::Ready(None) => Ok(Async::Ready(None)),
Async::Ready(Some(f)) => {
Ok(Async::Ready(Some(
SwallowErrors(f.into_future(), PhantomData)
)))
}
Async::NotReady => Ok(Async::NotReady),
}
}
}