@@ -17,9 +17,9 @@ pub use builder::AcceptorBuilder;
17
17
use builder:: WantsTlsConfig ;
18
18
19
19
/// A TLS acceptor that can be used with hyper servers.
20
- pub struct TlsAcceptor {
20
+ pub struct TlsAcceptor < A = AddrIncoming > {
21
21
config : Arc < ServerConfig > ,
22
- incoming : AddrIncoming ,
22
+ acceptor : A ,
23
23
}
24
24
25
25
/// An Acceptor for the `https` scheme.
@@ -31,20 +31,23 @@ impl TlsAcceptor {
31
31
32
32
/// Creates a new `TlsAcceptor` from a `ServerConfig` and an `AddrIncoming`.
33
33
pub fn new ( config : Arc < ServerConfig > , incoming : AddrIncoming ) -> Self {
34
- Self { config, incoming }
34
+ Self { config, acceptor : incoming }
35
35
}
36
36
}
37
37
38
- impl Accept for TlsAcceptor {
39
- type Conn = TlsStream ;
38
+ impl < A > Accept for TlsAcceptor < A >
39
+ where A : Accept < Error = io:: Error > + Unpin ,
40
+ A :: Conn : AsyncRead + AsyncWrite + Unpin ,
41
+ {
42
+ type Conn = TlsStream < A :: Conn > ;
40
43
type Error = io:: Error ;
41
44
42
45
fn poll_accept (
43
46
self : Pin < & mut Self > ,
44
47
cx : & mut Context < ' _ > ,
45
48
) -> Poll < Option < Result < Self :: Conn , Self :: Error > > > {
46
49
let pin = self . get_mut ( ) ;
47
- Poll :: Ready ( match ready ! ( Pin :: new( & mut pin. incoming ) . poll_accept( cx) ) {
50
+ Poll :: Ready ( match ready ! ( Pin :: new( & mut pin. acceptor ) . poll_accept( cx) ) {
48
51
Some ( Ok ( sock) ) => Some ( Ok ( TlsStream :: new ( sock, pin. config . clone ( ) ) ) ) ,
49
52
Some ( Err ( e) ) => Some ( Err ( e) ) ,
50
53
None => None ,
@@ -66,22 +69,24 @@ where
66
69
// tokio_rustls::server::TlsStream doesn't expose constructor methods,
67
70
// so we have to TlsAcceptor::accept and handshake to have access to it
68
71
// TlsStream implements AsyncRead/AsyncWrite by handshaking with tokio_rustls::Accept first
69
- pub struct TlsStream {
70
- state : State ,
72
+ pub struct TlsStream < C = AddrStream > {
73
+ state : State < C > ,
71
74
}
72
75
73
- impl TlsStream {
74
- fn new ( stream : AddrStream , config : Arc < ServerConfig > ) -> Self {
76
+
77
+ impl < C > TlsStream < C >
78
+ where C : AsyncRead + AsyncWrite + Unpin
79
+ {
80
+ fn new ( stream : C , config : Arc < ServerConfig > ) -> Self {
75
81
let accept = tokio_rustls:: TlsAcceptor :: from ( config) . accept ( stream) ;
76
82
Self {
77
83
state : State :: Handshaking ( accept) ,
78
84
}
79
85
}
80
-
81
86
/// Returns a reference to the underlying IO stream.
82
87
///
83
88
/// This should always return `Some`, except if an error has already been yielded.
84
- pub fn io ( & self ) -> Option < & AddrStream > {
89
+ pub fn io ( & self ) -> Option < & C > {
85
90
match & self . state {
86
91
State :: Handshaking ( accept) => accept. get_ref ( ) ,
87
92
State :: Streaming ( stream) => Some ( stream. get_ref ( ) . 0 ) ,
@@ -99,7 +104,9 @@ impl TlsStream {
99
104
}
100
105
}
101
106
102
- impl AsyncRead for TlsStream {
107
+ impl < C > AsyncRead for TlsStream < C >
108
+ where C : AsyncRead + AsyncWrite + Unpin
109
+ {
103
110
fn poll_read (
104
111
self : Pin < & mut Self > ,
105
112
cx : & mut Context ,
@@ -122,7 +129,9 @@ impl AsyncRead for TlsStream {
122
129
}
123
130
}
124
131
125
- impl AsyncWrite for TlsStream {
132
+ impl < C > AsyncWrite for TlsStream < C >
133
+ where C : AsyncRead + AsyncWrite + Unpin
134
+ {
126
135
fn poll_write (
127
136
self : Pin < & mut Self > ,
128
137
cx : & mut Context < ' _ > ,
@@ -159,7 +168,7 @@ impl AsyncWrite for TlsStream {
159
168
}
160
169
}
161
170
162
- enum State {
163
- Handshaking ( tokio_rustls:: Accept < AddrStream > ) ,
164
- Streaming ( tokio_rustls:: server:: TlsStream < AddrStream > ) ,
171
+ enum State < C > {
172
+ Handshaking ( tokio_rustls:: Accept < C > ) ,
173
+ Streaming ( tokio_rustls:: server:: TlsStream < C > ) ,
165
174
}
0 commit comments