@@ -13,14 +13,13 @@ use http::{Request, Response};
13
13
use http_body:: Body ;
14
14
use hyper:: {
15
15
body:: Incoming ,
16
- rt:: { bounds:: Http2ServerConnExec , Timer } ,
16
+ rt:: { bounds:: Http2ServerConnExec , Read , ReadBuf , Timer , Write } ,
17
17
server:: conn:: { http1, http2} ,
18
18
service:: Service ,
19
19
} ;
20
20
use pin_project_lite:: pin_project;
21
- use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
22
21
23
- use crate :: { common:: rewind:: Rewind , rt :: TokioIo } ;
22
+ use crate :: common:: rewind:: Rewind ;
24
23
25
24
type Result < T > = std:: result:: Result < T , Box < dyn std:: error:: Error + Send + Sync > > ;
26
25
@@ -74,11 +73,10 @@ impl<E> Builder<E> {
74
73
B : Body + Send + ' static ,
75
74
B :: Data : Send ,
76
75
B :: Error : Into < Box < dyn StdError + Send + Sync > > ,
77
- I : AsyncRead + AsyncWrite + Unpin + ' static ,
76
+ I : Read + Write + Unpin + ' static ,
78
77
E : Http2ServerConnExec < S :: Future , B > ,
79
78
{
80
79
let ( version, io) = read_version ( io) . await ?;
81
- let io = TokioIo :: new ( io) ;
82
80
match version {
83
81
Version :: H1 => self . http1 . serve_connection ( io, service) . await ?,
84
82
Version :: H2 => self . http2 . serve_connection ( io, service) . await ?,
@@ -98,11 +96,10 @@ impl<E> Builder<E> {
98
96
B : Body + Send + ' static ,
99
97
B :: Data : Send ,
100
98
B :: Error : Into < Box < dyn StdError + Send + Sync > > ,
101
- I : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
99
+ I : Read + Write + Unpin + Send + ' static ,
102
100
E : Http2ServerConnExec < S :: Future , B > ,
103
101
{
104
102
let ( version, io) = read_version ( io) . await ?;
105
- let io = TokioIo :: new ( io) ;
106
103
match version {
107
104
Version :: H1 => {
108
105
self . http1
@@ -123,12 +120,14 @@ enum Version {
123
120
}
124
121
async fn read_version < ' a , A > ( mut reader : A ) -> IoResult < ( Version , Rewind < A > ) >
125
122
where
126
- A : AsyncRead + Unpin ,
123
+ A : Read + Unpin ,
127
124
{
128
- let mut buf = [ 0 ; 24 ] ;
125
+ use std:: mem:: MaybeUninit ;
126
+
127
+ let mut buf = [ MaybeUninit :: uninit ( ) ; 24 ] ;
129
128
let ( version, buf) = ReadVersion {
130
129
reader : & mut reader,
131
- buf : ReadBuf :: new ( & mut buf) ,
130
+ buf : ReadBuf :: uninit ( & mut buf) ,
132
131
version : Version :: H1 ,
133
132
_pin : PhantomPinned ,
134
133
}
@@ -148,21 +147,21 @@ pin_project! {
148
147
149
148
impl < A > Future for ReadVersion < ' _ , A >
150
149
where
151
- A : AsyncRead + Unpin + ?Sized ,
150
+ A : Read + Unpin + ?Sized ,
152
151
{
153
152
type Output = IoResult < ( Version , Vec < u8 > ) > ;
154
153
155
154
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < IoResult < ( Version , Vec < u8 > ) > > {
156
155
let this = self . project ( ) ;
157
156
158
- while this. buf . remaining ( ) != 0 {
157
+ while this. buf . filled ( ) . len ( ) < H2_PREFACE . len ( ) {
159
158
if this. buf . filled ( ) != & H2_PREFACE [ 0 ..this. buf . filled ( ) . len ( ) ] {
160
159
return Poll :: Ready ( Ok ( ( * this. version , this. buf . filled ( ) . to_vec ( ) ) ) ) ;
161
160
}
162
161
// if our buffer is empty, then we need to read some data to continue.
163
- let rem = this. buf . remaining ( ) ;
164
- ready ! ( Pin :: new( & mut * this. reader) . poll_read( cx, this. buf) ) ?;
165
- if this. buf . remaining ( ) == rem {
162
+ let len = this. buf . filled ( ) . len ( ) ;
163
+ ready ! ( Pin :: new( & mut * this. reader) . poll_read( cx, this. buf. unfilled ( ) ) ) ?;
164
+ if this. buf . filled ( ) . len ( ) == len {
166
165
return Err ( IoError :: new ( ErrorKind :: UnexpectedEof , "early eof" ) ) . into ( ) ;
167
166
}
168
167
}
@@ -302,7 +301,7 @@ impl<E> Http1Builder<'_, E> {
302
301
B : Body + Send + ' static ,
303
302
B :: Data : Send ,
304
303
B :: Error : Into < Box < dyn StdError + Send + Sync > > ,
305
- I : AsyncRead + AsyncWrite + Unpin + ' static ,
304
+ I : Read + Write + Unpin + ' static ,
306
305
E : Http2ServerConnExec < S :: Future , B > ,
307
306
{
308
307
self . inner . serve_connection ( io, service) . await
@@ -450,7 +449,7 @@ impl<E> Http2Builder<'_, E> {
450
449
B : Body + Send + ' static ,
451
450
B :: Data : Send ,
452
451
B :: Error : Into < Box < dyn StdError + Send + Sync > > ,
453
- I : AsyncRead + AsyncWrite + Unpin + ' static ,
452
+ I : Read + Write + Unpin + ' static ,
454
453
E : Http2ServerConnExec < S :: Future , B > ,
455
454
{
456
455
self . inner . serve_connection ( io, service) . await
@@ -562,6 +561,7 @@ mod tests {
562
561
tokio:: spawn ( async move {
563
562
loop {
564
563
let ( stream, _) = listener. accept ( ) . await . unwrap ( ) ;
564
+ let stream = TokioIo :: new ( stream) ;
565
565
tokio:: task:: spawn ( async move {
566
566
let _ = auto:: Builder :: new ( TokioExecutor :: new ( ) )
567
567
. serve_connection ( stream, service_fn ( hello) )
0 commit comments