@@ -566,44 +566,52 @@ impl AsyncWrite for PollDataChannel {
566566 return Poll :: Ready ( Ok ( 0 ) ) ;
567567 }
568568
569- let ( fut, fut_is_new) = match self . write_fut . as_mut ( ) {
570- Some ( fut) => ( fut, false ) ,
571- None => {
572- let data_channel = self . data_channel . clone ( ) ;
573- let bytes = Bytes :: copy_from_slice ( buf) ;
574- (
575- self . write_fut
576- . get_or_insert ( Box :: pin ( async move { data_channel. write ( & bytes) . await } ) ) ,
577- true ,
578- )
569+ if let Some ( fut) = self . write_fut . as_mut ( ) {
570+ match fut. as_mut ( ) . poll ( cx) {
571+ Poll :: Pending => Poll :: Pending ,
572+ Poll :: Ready ( Err ( e) ) => {
573+ let data_channel = self . data_channel . clone ( ) ;
574+ let bytes = Bytes :: copy_from_slice ( buf) ;
575+ self . write_fut =
576+ Some ( Box :: pin ( async move { data_channel. write ( & bytes) . await } ) ) ;
577+ Poll :: Ready ( Err ( e. into ( ) ) )
578+ }
579+ // Given the data is buffered, it's okay to ignore the number of written bytes.
580+ //
581+ // TODO: In the long term, `data_channel.write` should be made sync. Then we could
582+ // remove the whole `if` condition and just call `data_channel.write`.
583+ Poll :: Ready ( Ok ( _) ) => {
584+ let data_channel = self . data_channel . clone ( ) ;
585+ let bytes = Bytes :: copy_from_slice ( buf) ;
586+ self . write_fut =
587+ Some ( Box :: pin ( async move { data_channel. write ( & bytes) . await } ) ) ;
588+ Poll :: Ready ( Ok ( buf. len ( ) ) )
589+ }
579590 }
580- } ;
591+ } else {
592+ let data_channel = self . data_channel . clone ( ) ;
593+ let bytes = Bytes :: copy_from_slice ( buf) ;
594+ let fut = self
595+ . write_fut
596+ . insert ( Box :: pin ( async move { data_channel. write ( & bytes) . await } ) ) ;
581597
582- match fut. as_mut ( ) . poll ( cx) {
583- Poll :: Pending => {
598+ match fut. as_mut ( ) . poll ( cx) {
584599 // If it's the first time we're polling the future, `Poll::Pending` can't be
585- // returned because that would mean the `PollStream ` is not ready for writing. And
586- // this is not true since we've just created a future, which is going to write the
587- // buf to the underlying stream.
600+ // returned because that would mean the `PollDataChannel ` is not ready for writing.
601+ // And this is not true since we've just created a future, which is going to write
602+ // the buf to the underlying stream.
588603 //
589604 // It's okay to return `Poll::Ready` if the data is buffered (this is what the
590605 // buffered writer and `File` do).
591- if fut_is_new {
592- Poll :: Ready ( Ok ( buf. len ( ) ) )
593- } else {
594- // If it's the subsequent poll, it's okay to return `Poll::Pending` as it
595- // indicates that the `PollStream` is not ready for writing. Only one future
596- // can be in progress at the time.
597- Poll :: Pending
606+ Poll :: Pending => Poll :: Ready ( Ok ( buf. len ( ) ) ) ,
607+ Poll :: Ready ( Err ( e) ) => {
608+ self . write_fut = None ;
609+ Poll :: Ready ( Err ( e. into ( ) ) )
610+ }
611+ Poll :: Ready ( Ok ( n) ) => {
612+ self . write_fut = None ;
613+ Poll :: Ready ( Ok ( n) )
598614 }
599- }
600- Poll :: Ready ( Err ( e) ) => {
601- self . write_fut = None ;
602- Poll :: Ready ( Err ( e. into ( ) ) )
603- }
604- Poll :: Ready ( Ok ( n) ) => {
605- self . write_fut = None ;
606- Poll :: Ready ( Ok ( n) )
607615 }
608616 }
609617 }
0 commit comments