tk_listen/
traits.rs

1use std::time::Duration;
2
3use futures::{Stream, IntoFuture};
4
5use sleep_on_error;
6use listen;
7
8
9/// An extension trait that provides necessary combinators for turning
10/// a stream of `accept()` events into a full-featured connection listener
11///
12/// Usually both `sleep_on_error` and `listen` commbinators are used in pair
13/// with `.map()` in-between. See [examples][1] for full-featured example.
14///
15/// [1]: https://github.com/tailhook/tk-listen/tree/master/examples
16pub trait ListenExt: Stream {
17    /// Turns a listening stream that you can get from `TcpListener::incoming`
18    /// into a stream that supresses errors and sleeps on resource shortage,
19    /// effectively allowing listening stream to resume on error.
20    fn sleep_on_error(self, delay: Duration)
21        -> sleep_on_error::SleepOnError<Self>
22        where Self: Sized,
23    {
24        sleep_on_error::new(self, delay)
25    }
26    /// Turns a stream of protocol handlers usually produced by mapping
27    /// a stream of accepted cnnec
28    fn listen(self, max_connections: usize) -> listen::Listen<Self>
29        where Self: Sized,
30              Self::Item: IntoFuture<Item=(), Error=()>,
31    {
32        listen::new(self, max_connections)
33    }
34}
35
36impl<T: Stream> ListenExt for T {}