@@ -4,7 +4,7 @@ use std::{
4
4
task:: { Context , Poll } ,
5
5
} ;
6
6
7
- use bytes:: { Buf , Bytes } ;
7
+ use bytes:: { Buf , Bytes , BytesMut } ;
8
8
use http:: HeaderMap ;
9
9
use http_body:: { Body , Frame } ;
10
10
@@ -38,6 +38,11 @@ impl<B: Buf> Collected<B> {
38
38
self . bufs . copy_to_bytes ( self . bufs . remaining ( ) )
39
39
}
40
40
41
+ /// Convert this body into a [`BytesMut`].
42
+ pub fn to_bytes_mut ( mut self ) -> BytesMut {
43
+ self . bufs . copy_to_bytes_mut ( self . bufs . remaining ( ) )
44
+ }
45
+
41
46
pub ( crate ) fn push_frame ( & mut self , frame : Frame < B > ) {
42
47
let frame = match frame. into_data ( ) {
43
48
Ok ( data) => {
@@ -125,6 +130,18 @@ mod tests {
125
130
assert_eq ! ( & buf. copy_to_bytes( buf. remaining( ) ) [ ..] , b"helloworld!" ) ;
126
131
}
127
132
133
+ #[ tokio:: test]
134
+ async fn segmented_body_mut ( ) {
135
+ let bufs = [ & b"hello" [ ..] , & b"world" [ ..] , & b"!" [ ..] ] ;
136
+ let body = StreamBody :: new ( stream:: iter ( bufs. map ( Frame :: data) . map ( Ok :: < _ , Infallible > ) ) ) ;
137
+
138
+ let buffered = body. collect ( ) . await . unwrap ( ) ;
139
+
140
+ let mut buf = buffered. to_bytes_mut ( ) ;
141
+
142
+ assert_eq ! ( & buf. copy_to_bytes( buf. remaining( ) ) [ ..] , b"helloworld!" ) ;
143
+ }
144
+
128
145
#[ tokio:: test]
129
146
async fn delayed_segments ( ) {
130
147
let one = stream:: once ( async { Ok :: < _ , Infallible > ( Frame :: data ( & b"hello " [ ..] ) ) } ) ;
0 commit comments