File tree Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change 1
1
use bytes:: Bytes ;
2
2
use futures:: stream:: StreamExt ;
3
- use futures:: stream:: TryStreamExt ;
4
3
use httpbis:: Client ;
5
4
use httpbis:: ClientIntf ;
6
5
use httpbis:: Headers ;
@@ -70,11 +69,8 @@ fn request() {
70
69
. expect ( "headers" ) ;
71
70
assert_eq ! ( 200 , header. status( ) ) ;
72
71
73
- // TODO: check content
74
- body. into_stream ( )
75
- . try_collect :: < Vec < _ > > ( )
76
- . await
77
- . expect ( "body" ) ;
72
+ let body = body. collect_data ( ) . await . expect ( "body" ) ;
73
+ assert_eq ! ( & b"hello there" [ ..] , & body[ ..] ) ;
78
74
} )
79
75
} ) ;
80
76
}
Original file line number Diff line number Diff line change @@ -7,13 +7,16 @@ use std::pin::Pin;
7
7
use std:: task:: Context ;
8
8
use std:: task:: Poll ;
9
9
10
+ use bytes:: BufMut ;
10
11
use bytes:: Bytes ;
12
+ use bytes:: BytesMut ;
11
13
use futures:: future;
12
14
use futures:: Future ;
13
15
use futures:: Stream ;
14
16
use tokio:: io:: AsyncRead ;
15
17
use tokio:: io:: ReadBuf ;
16
18
19
+ use crate :: solicit_async:: TryFutureBox ;
17
20
use crate :: solicit_async:: TryStreamBox ;
18
21
use crate :: DataOrTrailers ;
19
22
use crate :: Headers ;
@@ -71,7 +74,7 @@ pub trait StreamAfterHeaders: fmt::Debug + Unpin + Send + 'static {
71
74
/// Fetch next `DATA`.
72
75
fn next_data < ' a > (
73
76
& ' a mut self ,
74
- ) -> Pin < Box < dyn Future < Output = crate :: Result < Option < Bytes > > > + ' a > > {
77
+ ) -> Pin < Box < dyn Future < Output = crate :: Result < Option < Bytes > > > + Send + ' a > > {
75
78
Box :: pin ( future:: poll_fn ( move |cx| self . poll_data ( cx) ?. map ( Ok ) ) )
76
79
}
77
80
@@ -105,6 +108,24 @@ pub trait StreamAfterHeaders: fmt::Debug + Unpin + Send + 'static {
105
108
{
106
109
Box :: pin ( DataStream ( self ) )
107
110
}
111
+
112
+ fn collect_data ( mut self ) -> TryFutureBox < Bytes >
113
+ where
114
+ Self : Sized ,
115
+ {
116
+ Box :: pin ( async move {
117
+ let mut r = BytesMut :: new ( ) ;
118
+ loop {
119
+ match self . next_data ( ) . await ? {
120
+ None => return Ok ( r. freeze ( ) ) ,
121
+ Some ( b) => {
122
+ // TODO: figure out how to efficiently extend from Bytes
123
+ r. put_slice ( & b)
124
+ }
125
+ }
126
+ }
127
+ } )
128
+ }
108
129
}
109
130
110
131
struct AsStream < S : StreamAfterHeaders > ( S ) ;
You can’t perform that action at this time.
0 commit comments