1- use crate :: { bytes_ext :: BytesExt , cursors:: RawCursor , error:: Result , response:: Response } ;
2- use bytes:: { Bytes , BytesMut } ;
1+ use crate :: { cursors:: RawCursor , error:: Result , response:: Response } ;
2+ use bytes:: { Buf , Bytes , BytesMut } ;
33use std:: {
44 io:: Result as IoResult ,
55 pin:: Pin ,
@@ -33,7 +33,7 @@ use tokio::io::{AsyncBufRead, AsyncRead, ReadBuf};
3333/// [`Query::fetch_bytes`]: crate::query::Query::fetch_bytes
3434pub struct BytesCursor {
3535 raw : RawCursor ,
36- bytes : BytesExt ,
36+ bytes : Bytes ,
3737}
3838
3939// TODO: what if any next/poll_* called AFTER error returned?
@@ -42,7 +42,7 @@ impl BytesCursor {
4242 pub ( crate ) fn new ( response : Response ) -> Self {
4343 Self {
4444 raw : RawCursor :: new ( response) ,
45- bytes : BytesExt :: default ( ) ,
45+ bytes : Bytes :: default ( ) ,
4646 }
4747 }
4848
@@ -82,17 +82,19 @@ impl BytesCursor {
8282
8383 #[ cold]
8484 fn poll_refill ( & mut self , cx : & mut Context < ' _ > ) -> Poll < IoResult < bool > > {
85- debug_assert_eq ! ( self . bytes. remaining( ) , 0 ) ;
86-
87- // TODO: should we repeat if `poll_next` returns an empty buffer?
88-
89- match ready ! ( self . raw. poll_next( cx) ?) {
90- Some ( chunk) => {
91- self . bytes . extend ( chunk) ;
92- Poll :: Ready ( Ok ( true ) )
85+ debug_assert_eq ! ( self . bytes. len( ) , 0 ) ;
86+
87+ // Theoretically, `self.raw.poll_next(cx)` can return empty chunks.
88+ // In this case, we should continue polling until we get a non-empty chunk or
89+ // end of stream in order to avoid false positive `Ok(0)` in I/O traits.
90+ while self . bytes . is_empty ( ) {
91+ match ready ! ( self . raw. poll_next( cx) ?) {
92+ Some ( chunk) => self . bytes = chunk,
93+ None => return Poll :: Ready ( Ok ( false ) ) ,
9394 }
94- None => Poll :: Ready ( Ok ( false ) ) ,
9595 }
96+
97+ Poll :: Ready ( Ok ( true ) )
9698 }
9799
98100 /// Returns the total size in bytes received from the CH server since
@@ -125,9 +127,9 @@ impl AsyncRead for BytesCursor {
125127 break ;
126128 }
127129
128- let bytes = self . bytes . slice ( ) ;
129- let len = bytes. len ( ) . min ( buf . remaining ( ) ) ;
130- buf. put_slice ( & bytes[ ..len] ) ;
130+ let len = self . bytes . len ( ) . min ( buf . remaining ( ) ) ;
131+ let bytes = self . bytes . slice ( ..len ) ;
132+ buf. put_slice ( & bytes[ 0 ..len] ) ;
131133 self . bytes . advance ( len) ;
132134 }
133135
@@ -138,17 +140,17 @@ impl AsyncRead for BytesCursor {
138140impl AsyncBufRead for BytesCursor {
139141 #[ inline]
140142 fn poll_fill_buf ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < IoResult < & [ u8 ] > > {
141- if self . bytes . is_empty ( ) && ! ready ! ( self . poll_refill ( cx ) ? ) {
142- return Poll :: Ready ( Ok ( & [ ] ) ) ;
143+ if self . bytes . is_empty ( ) {
144+ ready ! ( self . poll_refill ( cx ) ? ) ;
143145 }
144146
145- Poll :: Ready ( Ok ( self . get_mut ( ) . bytes . slice ( ) ) )
147+ Poll :: Ready ( Ok ( & self . get_mut ( ) . bytes ) )
146148 }
147149
148150 #[ inline]
149151 fn consume ( mut self : Pin < & mut Self > , amt : usize ) {
150152 assert ! (
151- amt <= self . bytes. remaining ( ) ,
153+ amt <= self . bytes. len ( ) ,
152154 "invalid `AsyncBufRead::consume` usage"
153155 ) ;
154156 self . bytes . advance ( amt) ;
0 commit comments