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 std::io;

use futures::TryFutureExt;
use tokio_util::either::Either;

use crate::listener::{Connection, Endpoint};

pub trait Listener: Sized + Send + Sync {
    type Accept: Send;

    type Connection: Connection;

    #[crate::async_bound(Send)]
    async fn accept(&self) -> io::Result<Self::Accept>;

    #[crate::async_bound(Send)]
    async fn connect(&self, accept: Self::Accept) -> io::Result<Self::Connection>;

    fn endpoint(&self) -> io::Result<Endpoint>;
}

impl<L: Listener> Listener for &L {
    type Accept = L::Accept;

    type Connection = L::Connection;

    async fn accept(&self) -> io::Result<Self::Accept> {
        <L as Listener>::accept(self).await
    }

    async fn connect(&self, accept: Self::Accept) -> io::Result<Self::Connection> {
        <L as Listener>::connect(self, accept).await
    }

    fn endpoint(&self) -> io::Result<Endpoint> {
        <L as Listener>::endpoint(self)
    }
}

impl<A: Listener, B: Listener> Listener for Either<A, B> {
    type Accept = Either<A::Accept, B::Accept>;

    type Connection = Either<A::Connection, B::Connection>;

    async fn accept(&self) -> io::Result<Self::Accept> {
        match self {
            Either::Left(l) => l.accept().map_ok(Either::Left).await,
            Either::Right(l) => l.accept().map_ok(Either::Right).await,
        }
    }

    async fn connect(&self, accept: Self::Accept) -> io::Result<Self::Connection>  {
        match (self, accept) {
            (Either::Left(l), Either::Left(a)) => l.connect(a).map_ok(Either::Left).await,
            (Either::Right(l), Either::Right(a)) => l.connect(a).map_ok(Either::Right).await,
            _ => unreachable!()
        }
    }

    fn endpoint(&self) -> io::Result<Endpoint> {
        match self {
            Either::Left(l) => l.endpoint(),
            Either::Right(l) => l.endpoint(),
        }
    }
}