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
use std::rc::Rc;
use std::sync::Arc;
use futures::Future;
use tokio_core::reactor::Handle;

use super::endpoint::Endpoint;


/// A factory of `Endpoint`
pub trait NewEndpoint {
    /// The return type of `Endpoint`
    type Item;

    /// The error type of `Endpoint`
    type Error;

    /// The future type of `Endpoint`
    type Future: Future<Item = Self::Item, Error = Self::Error>;

    /// The type of `Endpoint` returned from `new_endpoint()`
    type Endpoint: Endpoint<Item = Self::Item, Error = Self::Error, Future = Self::Future>;

    /// Create a new instance of `Endpoint` with given event loop
    fn new_endpoint(&self, handle: &Handle) -> Self::Endpoint;
}

impl<F, E> NewEndpoint for F
where
    F: Fn(&Handle) -> E,
    E: Endpoint,
{
    type Item = E::Item;
    type Error = E::Error;
    type Future = E::Future;
    type Endpoint = E;

    fn new_endpoint(&self, handle: &Handle) -> Self::Endpoint {
        (*self)(handle)
    }
}

impl<E: NewEndpoint> NewEndpoint for Rc<E> {
    type Item = E::Item;
    type Error = E::Error;
    type Future = E::Future;
    type Endpoint = E::Endpoint;

    fn new_endpoint(&self, handle: &Handle) -> Self::Endpoint {
        (**self).new_endpoint(handle)
    }
}

impl<E: NewEndpoint> NewEndpoint for Arc<E> {
    type Item = E::Item;
    type Error = E::Error;
    type Future = E::Future;
    type Endpoint = E::Endpoint;

    fn new_endpoint(&self, handle: &Handle) -> Self::Endpoint {
        (**self).new_endpoint(handle)
    }
}