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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use super::*;
use ::hyper::server::accept::Accept as HyperAccept;
use ::hyper::server::conn::{AddrIncoming, AddrStream};
use std::ops::{Deref, DerefMut};

#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-h1", feature = "hyper-h2"))))]
impl AsyncAccept for AddrIncoming {
    type Connection = AddrStream;
    type Error = std::io::Error;

    fn poll_accept(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Self::Connection, Self::Error>>> {
        <AddrIncoming as HyperAccept>::poll_accept(self, cx)
    }
}

pin_project! {
    /// newtype for a [`::hyper::server::accept::Accept`] to impl [`AsyncAccept`]
    ///
    /// Unfortunately, it isn't possible to use a blanket impl, due to coherence rules.
    /// At least until [RFC 1210](https://rust-lang.github.io/rfcs/1210-impl-specialization.html)
    /// (specialization) is stabilized.
    //#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-h1", feature = "hyper-h2"))))]
    pub struct WrappedAccept<A> {
        // sadly, pin-project-lite doesn't suport tuple structs :(

        #[pin]
        inner: A
    }
}

/// Wrap any[`::hyper::server::accept::Accept`] as an [`AsyncAccept`].
///
/// This allows you to use any type that implements the hyper `Accept` interface
/// in a [`TlsListener`].
pub fn wrap<A: HyperAccept>(acceptor: A) -> WrappedAccept<A> {
    WrappedAccept { inner: acceptor }
}

#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-h1", feature = "hyper-h2"))))]
impl<A: HyperAccept> AsyncAccept for WrappedAccept<A>
where
    A::Conn: AsyncRead + AsyncWrite,
{
    type Connection = A::Conn;
    type Error = A::Error;

    fn poll_accept(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Self::Connection, Self::Error>>> {
        self.project().inner.poll_accept(cx)
    }
}

impl<A: HyperAccept> Deref for WrappedAccept<A> {
    type Target = A;
    fn deref(&self) -> &A {
        &self.inner
    }
}

impl<A: HyperAccept> DerefMut for WrappedAccept<A> {
    fn deref_mut(&mut self) -> &mut A {
        &mut self.inner
    }
}

impl<A: HyperAccept> WrappedAccept<A> {
    /// Conver to the object wrapped by this `WrappedAccept`
    pub fn into_inner(self) -> A {
        self.inner
    }
}

impl<A: HyperAccept, T> TlsListener<WrappedAccept<A>, T>
where
    A::Conn: AsyncWrite + AsyncRead,
    T: AsyncTls<A::Conn>,
{
    /// Create a `TlsListener` from a hyper [`Accept`](::hyper::server::accept::Accept) and tls
    /// acceptor.
    pub fn new_hyper(tls: T, listener: A) -> Self {
        Self::new(tls, wrap(listener))
    }
}

#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-h1", feature = "hyper-h2"))))]
impl<A, T> HyperAccept for TlsListener<A, T>
where
    A: AsyncAccept,
    A::Error: std::error::Error,
    T: AsyncTls<A::Connection>,
{
    type Conn = T::Stream;
    type Error = Error<A::Error, T::Error>;

    fn poll_accept(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
        self.poll_next(cx)
    }
}