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