@@ -79,3 +79,149 @@ impl Future for TokioSleep {
79
79
// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html
80
80
81
81
impl Sleep for TokioSleep { }
82
+
83
+ pin_project ! {
84
+ #[ derive( Debug ) ]
85
+ pub struct TokioIo <T > {
86
+ #[ pin]
87
+ inner: T ,
88
+ }
89
+ }
90
+
91
+ impl < T > TokioIo < T > {
92
+ pub fn new ( inner : T ) -> Self {
93
+ Self { inner }
94
+ }
95
+
96
+ pub fn inner ( self ) -> T {
97
+ self . inner
98
+ }
99
+ }
100
+
101
+ impl < T > hyper:: rt:: Read for TokioIo < T >
102
+ where
103
+ T : tokio:: io:: AsyncRead ,
104
+ {
105
+ fn poll_read (
106
+ self : Pin < & mut Self > ,
107
+ cx : & mut Context < ' _ > ,
108
+ mut buf : hyper:: rt:: ReadBufCursor < ' _ > ,
109
+ ) -> Poll < Result < ( ) , std:: io:: Error > > {
110
+ let n = unsafe {
111
+ let mut tbuf = tokio:: io:: ReadBuf :: uninit ( buf. as_mut ( ) ) ;
112
+ match tokio:: io:: AsyncRead :: poll_read ( self . project ( ) . inner , cx, & mut tbuf) {
113
+ Poll :: Ready ( Ok ( ( ) ) ) => tbuf. filled ( ) . len ( ) ,
114
+ other => return other,
115
+ }
116
+ } ;
117
+
118
+ unsafe {
119
+ buf. advance ( n) ;
120
+ }
121
+ Poll :: Ready ( Ok ( ( ) ) )
122
+ }
123
+ }
124
+
125
+ impl < T > hyper:: rt:: Write for TokioIo < T >
126
+ where
127
+ T : tokio:: io:: AsyncWrite ,
128
+ {
129
+ fn poll_write (
130
+ self : Pin < & mut Self > ,
131
+ cx : & mut Context < ' _ > ,
132
+ buf : & [ u8 ] ,
133
+ ) -> Poll < Result < usize , std:: io:: Error > > {
134
+ tokio:: io:: AsyncWrite :: poll_write ( self . project ( ) . inner , cx, buf)
135
+ }
136
+
137
+ fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , std:: io:: Error > > {
138
+ tokio:: io:: AsyncWrite :: poll_flush ( self . project ( ) . inner , cx)
139
+ }
140
+
141
+ fn poll_shutdown (
142
+ self : Pin < & mut Self > ,
143
+ cx : & mut Context < ' _ > ,
144
+ ) -> Poll < Result < ( ) , std:: io:: Error > > {
145
+ tokio:: io:: AsyncWrite :: poll_shutdown ( self . project ( ) . inner , cx)
146
+ }
147
+
148
+ fn is_write_vectored ( & self ) -> bool {
149
+ tokio:: io:: AsyncWrite :: is_write_vectored ( & self . inner )
150
+ }
151
+
152
+ fn poll_write_vectored (
153
+ self : Pin < & mut Self > ,
154
+ cx : & mut Context < ' _ > ,
155
+ bufs : & [ std:: io:: IoSlice < ' _ > ] ,
156
+ ) -> Poll < Result < usize , std:: io:: Error > > {
157
+ tokio:: io:: AsyncWrite :: poll_write_vectored ( self . project ( ) . inner , cx, bufs)
158
+ }
159
+ }
160
+
161
+ impl < T > tokio:: io:: AsyncRead for TokioIo < T >
162
+ where
163
+ T : hyper:: rt:: Read ,
164
+ {
165
+ fn poll_read (
166
+ self : Pin < & mut Self > ,
167
+ cx : & mut Context < ' _ > ,
168
+ tbuf : & mut tokio:: io:: ReadBuf < ' _ > ,
169
+ ) -> Poll < Result < ( ) , std:: io:: Error > > {
170
+ //let init = tbuf.initialized().len();
171
+ let filled = tbuf. filled ( ) . len ( ) ;
172
+ let sub_filled = unsafe {
173
+ let mut buf = hyper:: rt:: ReadBuf :: uninit ( tbuf. unfilled_mut ( ) ) ;
174
+
175
+ match hyper:: rt:: Read :: poll_read ( self . project ( ) . inner , cx, buf. unfilled ( ) ) {
176
+ Poll :: Ready ( Ok ( ( ) ) ) => buf. filled ( ) . len ( ) ,
177
+ other => return other,
178
+ }
179
+ } ;
180
+
181
+ let n_filled = filled + sub_filled;
182
+ // At least sub_filled bytes had to have been initialized.
183
+ let n_init = sub_filled;
184
+ unsafe {
185
+ tbuf. assume_init ( n_init) ;
186
+ tbuf. set_filled ( n_filled) ;
187
+ }
188
+
189
+ Poll :: Ready ( Ok ( ( ) ) )
190
+ }
191
+ }
192
+
193
+ impl < T > tokio:: io:: AsyncWrite for TokioIo < T >
194
+ where
195
+ T : hyper:: rt:: Write ,
196
+ {
197
+ fn poll_write (
198
+ self : Pin < & mut Self > ,
199
+ cx : & mut Context < ' _ > ,
200
+ buf : & [ u8 ] ,
201
+ ) -> Poll < Result < usize , std:: io:: Error > > {
202
+ hyper:: rt:: Write :: poll_write ( self . project ( ) . inner , cx, buf)
203
+ }
204
+
205
+ fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , std:: io:: Error > > {
206
+ hyper:: rt:: Write :: poll_flush ( self . project ( ) . inner , cx)
207
+ }
208
+
209
+ fn poll_shutdown (
210
+ self : Pin < & mut Self > ,
211
+ cx : & mut Context < ' _ > ,
212
+ ) -> Poll < Result < ( ) , std:: io:: Error > > {
213
+ hyper:: rt:: Write :: poll_shutdown ( self . project ( ) . inner , cx)
214
+ }
215
+
216
+ fn is_write_vectored ( & self ) -> bool {
217
+ hyper:: rt:: Write :: is_write_vectored ( & self . inner )
218
+ }
219
+
220
+ fn poll_write_vectored (
221
+ self : Pin < & mut Self > ,
222
+ cx : & mut Context < ' _ > ,
223
+ bufs : & [ std:: io:: IoSlice < ' _ > ] ,
224
+ ) -> Poll < Result < usize , std:: io:: Error > > {
225
+ hyper:: rt:: Write :: poll_write_vectored ( self . project ( ) . inner , cx, bufs)
226
+ }
227
+ }
0 commit comments