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