@@ -4,7 +4,7 @@ use std::{
44 task:: { Context , Poll } ,
55} ;
66
7- use bytes:: { Buf , Bytes } ;
7+ use bytes:: { Buf , Bytes , BytesMut } ;
88use http:: HeaderMap ;
99use http_body:: { Body , Frame } ;
1010
@@ -38,6 +38,11 @@ impl<B: Buf> Collected<B> {
3838 self . bufs . copy_to_bytes ( self . bufs . remaining ( ) )
3939 }
4040
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+
4146 pub ( crate ) fn push_frame ( & mut self , frame : Frame < B > ) {
4247 let frame = match frame. into_data ( ) {
4348 Ok ( data) => {
@@ -125,6 +130,18 @@ mod tests {
125130 assert_eq ! ( & buf. copy_to_bytes( buf. remaining( ) ) [ ..] , b"helloworld!" ) ;
126131 }
127132
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+
128145 #[ tokio:: test]
129146 async fn delayed_segments ( ) {
130147 let one = stream:: once ( async { Ok :: < _ , Infallible > ( Frame :: data ( & b"hello " [ ..] ) ) } ) ;
0 commit comments