11//! Compat wrappers for interop with other crates.
22
33use std:: {
4+ fmt:: Debug ,
45 io:: { self , BufRead , Read , Write } ,
56 mem:: MaybeUninit ,
67 pin:: Pin ,
78 task:: { Context , Poll } ,
89} ;
910
1011use compio_buf:: { BufResult , IntoInner , IoBuf , IoBufMut , SetBufInit } ;
11- use pin_project_lite:: pin_project;
1212
1313use crate :: { PinBoxFuture , buffer:: Buffer , util:: DEFAULT_BUF_SIZE } ;
1414
@@ -176,15 +176,14 @@ impl<S: crate::AsyncWrite> SyncStream<S> {
176176 }
177177}
178178
179- pin_project ! {
180- /// A stream wrapper for [`futures_util::io`] traits.
181- pub struct AsyncStream <S > {
182- #[ pin]
183- inner: SyncStream <S >,
184- read_future: Option <PinBoxFuture <io:: Result <usize >>>,
185- write_future: Option <PinBoxFuture <io:: Result <usize >>>,
186- shutdown_future: Option <PinBoxFuture <io:: Result <( ) >>>,
187- }
179+ /// A stream wrapper for [`futures_util::io`] traits.
180+ pub struct AsyncStream < S > {
181+ // The futures keep the reference to the inner stream, so we need to pin
182+ // the inner stream to make sure the reference is valid.
183+ inner : Pin < Box < SyncStream < S > > > ,
184+ read_future : Option < PinBoxFuture < io:: Result < usize > > > ,
185+ write_future : Option < PinBoxFuture < io:: Result < usize > > > ,
186+ shutdown_future : Option < PinBoxFuture < io:: Result < ( ) > > > ,
188187}
189188
190189impl < S > AsyncStream < S > {
@@ -200,7 +199,7 @@ impl<S> AsyncStream<S> {
200199
201200 fn new_impl ( inner : SyncStream < S > ) -> Self {
202201 Self {
203- inner,
202+ inner : Box :: pin ( inner ) ,
204203 read_future : None ,
205204 write_future : None ,
206205 shutdown_future : None ,
@@ -253,20 +252,18 @@ macro_rules! poll_future_would_block {
253252
254253impl < S : crate :: AsyncRead + ' static > futures_util:: AsyncRead for AsyncStream < S > {
255254 fn poll_read (
256- self : Pin < & mut Self > ,
255+ mut self : Pin < & mut Self > ,
257256 cx : & mut Context < ' _ > ,
258257 buf : & mut [ u8 ] ,
259258 ) -> Poll < io:: Result < usize > > {
260- let this = self . project ( ) ;
261259 // Safety:
262260 // - The futures won't live longer than the stream.
263- // - `self` is pinned.
264- // - The inner stream won't be moved.
261+ // - The inner stream is pinned.
265262 let inner: & ' static mut SyncStream < S > =
266- unsafe { & mut * ( this . inner . get_unchecked_mut ( ) as * mut _ ) } ;
263+ unsafe { & mut * ( self . inner . as_mut ( ) . get_unchecked_mut ( ) as * mut _ ) } ;
267264
268265 poll_future_would_block ! (
269- this . read_future,
266+ self . read_future,
270267 cx,
271268 inner. fill_read_buf( ) ,
272269 io:: Read :: read( inner, buf)
@@ -279,16 +276,14 @@ impl<S: crate::AsyncRead + 'static> AsyncStream<S> {
279276 ///
280277 /// On success, returns `Poll::Ready(Ok(num_bytes_read))`.
281278 pub fn poll_read_uninit (
282- self : Pin < & mut Self > ,
279+ mut self : Pin < & mut Self > ,
283280 cx : & mut Context < ' _ > ,
284281 buf : & mut [ MaybeUninit < u8 > ] ,
285282 ) -> Poll < io:: Result < usize > > {
286- let this = self . project ( ) ;
287-
288283 let inner: & ' static mut SyncStream < S > =
289- unsafe { & mut * ( this . inner . get_unchecked_mut ( ) as * mut _ ) } ;
284+ unsafe { & mut * ( self . inner . as_mut ( ) . get_unchecked_mut ( ) as * mut _ ) } ;
290285 poll_future_would_block ! (
291- this . read_future,
286+ self . read_future,
292287 cx,
293288 inner. fill_read_buf( ) ,
294289 inner. read_buf_uninit( buf)
@@ -297,79 +292,75 @@ impl<S: crate::AsyncRead + 'static> AsyncStream<S> {
297292}
298293
299294impl < S : crate :: AsyncRead + ' static > futures_util:: AsyncBufRead for AsyncStream < S > {
300- fn poll_fill_buf ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < & [ u8 ] > > {
301- let this = self . project ( ) ;
302-
295+ fn poll_fill_buf ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < & [ u8 ] > > {
303296 let inner: & ' static mut SyncStream < S > =
304- unsafe { & mut * ( this . inner . get_unchecked_mut ( ) as * mut _ ) } ;
297+ unsafe { & mut * ( self . inner . as_mut ( ) . get_unchecked_mut ( ) as * mut _ ) } ;
305298 poll_future_would_block ! (
306- this . read_future,
299+ self . read_future,
307300 cx,
308301 inner. fill_read_buf( ) ,
309302 // Safety: anyway the slice won't be used after free.
310303 io:: BufRead :: fill_buf( inner) . map( |slice| unsafe { & * ( slice as * const _) } )
311304 )
312305 }
313306
314- fn consume ( self : Pin < & mut Self > , amt : usize ) {
315- let this = self . project ( ) ;
316-
317- let inner: & ' static mut SyncStream < S > =
318- unsafe { & mut * ( this. inner . get_unchecked_mut ( ) as * mut _ ) } ;
319- inner. consume ( amt)
307+ fn consume ( mut self : Pin < & mut Self > , amt : usize ) {
308+ unsafe { self . inner . as_mut ( ) . get_unchecked_mut ( ) . consume ( amt) }
320309 }
321310}
322311
323312impl < S : crate :: AsyncWrite + ' static > futures_util:: AsyncWrite for AsyncStream < S > {
324313 fn poll_write (
325- self : Pin < & mut Self > ,
314+ mut self : Pin < & mut Self > ,
326315 cx : & mut Context < ' _ > ,
327316 buf : & [ u8 ] ,
328317 ) -> Poll < io:: Result < usize > > {
329- let this = self . project ( ) ;
330-
331- if this. shutdown_future . is_some ( ) {
332- debug_assert ! ( this. write_future. is_none( ) ) ;
318+ if self . shutdown_future . is_some ( ) {
319+ debug_assert ! ( self . write_future. is_none( ) ) ;
333320 return Poll :: Pending ;
334321 }
335322
336323 let inner: & ' static mut SyncStream < S > =
337- unsafe { & mut * ( this . inner . get_unchecked_mut ( ) as * mut _ ) } ;
324+ unsafe { & mut * ( self . inner . as_mut ( ) . get_unchecked_mut ( ) as * mut _ ) } ;
338325 poll_future_would_block ! (
339- this . write_future,
326+ self . write_future,
340327 cx,
341328 inner. flush_write_buf( ) ,
342329 io:: Write :: write( inner, buf)
343330 )
344331 }
345332
346- fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
347- let this = self . project ( ) ;
348-
349- if this. shutdown_future . is_some ( ) {
350- debug_assert ! ( this. write_future. is_none( ) ) ;
333+ fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
334+ if self . shutdown_future . is_some ( ) {
335+ debug_assert ! ( self . write_future. is_none( ) ) ;
351336 return Poll :: Pending ;
352337 }
353338
354339 let inner: & ' static mut SyncStream < S > =
355- unsafe { & mut * ( this . inner . get_unchecked_mut ( ) as * mut _ ) } ;
356- let res = poll_future ! ( this . write_future, cx, inner. flush_write_buf( ) ) ;
340+ unsafe { & mut * ( self . inner . as_mut ( ) . get_unchecked_mut ( ) as * mut _ ) } ;
341+ let res = poll_future ! ( self . write_future, cx, inner. flush_write_buf( ) ) ;
357342 Poll :: Ready ( res. map ( |_| ( ) ) )
358343 }
359344
360- fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
361- let this = self . project ( ) ;
362-
345+ fn poll_close ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
363346 // Avoid shutdown on flush because the inner buffer might be passed to the
364347 // driver.
365- if this . write_future . is_some ( ) {
366- debug_assert ! ( this . shutdown_future. is_none( ) ) ;
348+ if self . write_future . is_some ( ) {
349+ debug_assert ! ( self . shutdown_future. is_none( ) ) ;
367350 return Poll :: Pending ;
368351 }
369352
370353 let inner: & ' static mut SyncStream < S > =
371- unsafe { & mut * ( this . inner . get_unchecked_mut ( ) as * mut _ ) } ;
372- let res = poll_future ! ( this . shutdown_future, cx, inner. get_mut( ) . shutdown( ) ) ;
354+ unsafe { & mut * ( self . inner . as_mut ( ) . get_unchecked_mut ( ) as * mut _ ) } ;
355+ let res = poll_future ! ( self . shutdown_future, cx, inner. get_mut( ) . shutdown( ) ) ;
373356 Poll :: Ready ( res)
374357 }
375358}
359+
360+ impl < S : Debug > Debug for AsyncStream < S > {
361+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
362+ f. debug_struct ( "AsyncStream" )
363+ . field ( "inner" , & self . inner )
364+ . finish_non_exhaustive ( )
365+ }
366+ }
0 commit comments