@@ -125,39 +125,70 @@ impl<S> ByteReader<S> {
125
125
}
126
126
}
127
127
128
+ fn poll_read_helper < S > (
129
+ mut s : Pin < & mut ByteReader < S > > ,
130
+ cx : & mut Context < ' _ > ,
131
+ buf_len : usize ,
132
+ ) -> Poll < io:: Result < Option < Bytes > > >
133
+ where
134
+ S : Stream < Item = Result < Message , WsError > > + Unpin ,
135
+ {
136
+ Poll :: Ready ( Ok ( Some ( match s. bytes {
137
+ None => match Pin :: new ( & mut s. stream ) . poll_next ( cx) {
138
+ Poll :: Pending => return Poll :: Pending ,
139
+ Poll :: Ready ( None ) => return Poll :: Ready ( Ok ( None ) ) ,
140
+ Poll :: Ready ( Some ( Err ( e) ) ) => return Poll :: Ready ( Err ( convert_err ( e) ) ) ,
141
+ Poll :: Ready ( Some ( Ok ( msg) ) ) => {
142
+ let bytes = msg. into_data ( ) ;
143
+ if bytes. len ( ) > buf_len {
144
+ s. bytes . insert ( bytes) . split_to ( buf_len)
145
+ } else {
146
+ bytes
147
+ }
148
+ }
149
+ } ,
150
+ Some ( ref mut bytes) if bytes. len ( ) > buf_len => bytes. split_to ( buf_len) ,
151
+ Some ( ref mut bytes) => {
152
+ let bytes = bytes. clone ( ) ;
153
+ s. bytes = None ;
154
+ bytes
155
+ }
156
+ } ) ) )
157
+ }
158
+
128
159
impl < S > futures_io:: AsyncRead for ByteReader < S >
129
160
where
130
161
S : Stream < Item = Result < Message , WsError > > + Unpin ,
131
162
{
132
163
fn poll_read (
133
- mut self : Pin < & mut Self > ,
164
+ self : Pin < & mut Self > ,
134
165
cx : & mut Context < ' _ > ,
135
166
buf : & mut [ u8 ] ,
136
167
) -> Poll < io:: Result < usize > > {
137
- let buf_len = buf. len ( ) ;
138
- let bytes_to_read = match self . bytes {
139
- None => match Pin :: new ( & mut self . stream ) . poll_next ( cx) {
140
- Poll :: Pending => return Poll :: Pending ,
141
- Poll :: Ready ( None ) => return Poll :: Ready ( Ok ( 0 ) ) ,
142
- Poll :: Ready ( Some ( Err ( e) ) ) => return Poll :: Ready ( Err ( convert_err ( e) ) ) ,
143
- Poll :: Ready ( Some ( Ok ( msg) ) ) => {
144
- let bytes = msg. into_data ( ) ;
145
- if bytes. len ( ) > buf_len {
146
- self . bytes . insert ( bytes) . split_to ( buf_len)
147
- } else {
148
- bytes
149
- }
150
- }
151
- } ,
152
- Some ( ref mut bytes) if bytes. len ( ) > buf_len => bytes. split_to ( buf_len) ,
153
- Some ( ref mut bytes) => {
154
- let bytes = bytes. clone ( ) ;
155
- self . bytes = None ;
156
- bytes
168
+ poll_read_helper ( self , cx, buf. len ( ) ) . map_ok ( |bytes| {
169
+ bytes. map_or ( 0 , |bytes| {
170
+ buf[ ..bytes. len ( ) ] . copy_from_slice ( & bytes) ;
171
+ bytes. len ( )
172
+ } )
173
+ } )
174
+ }
175
+ }
176
+
177
+ #[ cfg( feature = "tokio-runtime" ) ]
178
+ impl < S > tokio:: io:: AsyncRead for ByteReader < S >
179
+ where
180
+ S : Stream < Item = Result < Message , WsError > > + Unpin ,
181
+ {
182
+ fn poll_read (
183
+ self : Pin < & mut Self > ,
184
+ cx : & mut Context < ' _ > ,
185
+ buf : & mut tokio:: io:: ReadBuf ,
186
+ ) -> Poll < io:: Result < ( ) > > {
187
+ poll_read_helper ( self , cx, buf. remaining ( ) ) . map_ok ( |bytes| {
188
+ if let Some ( ref bytes) = bytes {
189
+ buf. put_slice ( bytes) ;
157
190
}
158
- } ;
159
- buf. copy_from_slice ( & bytes_to_read) ;
160
- Poll :: Ready ( Ok ( bytes_to_read. len ( ) ) )
191
+ } )
161
192
}
162
193
}
163
194
0 commit comments