@@ -7,7 +7,7 @@ use super::{
77 status:: StatusCode ,
88 version:: Version ,
99} ;
10- use std:: fmt;
10+ use std:: { borrow :: Cow , fmt} ;
1111
1212type Result < T > = core:: result:: Result < T , Box < dyn std:: error:: Error > > ;
1313
@@ -33,7 +33,7 @@ type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
3333///
3434pub struct Response {
3535 head : ResponseParts ,
36- body : Vec < u8 > ,
36+ body : Cow < ' static , [ u8 ] > ,
3737}
3838
3939/// Component parts of an HTTP `Response`
@@ -67,7 +67,7 @@ pub struct Builder {
6767impl Response {
6868 /// Creates a new blank `Response` with the body
6969 #[ inline]
70- pub fn new ( body : Vec < u8 > ) -> Response {
70+ pub fn new ( body : Cow < ' static , [ u8 ] > ) -> Response {
7171 Response {
7272 head : ResponseParts :: new ( ) ,
7373 body,
@@ -81,7 +81,7 @@ impl Response {
8181 /// This API is used internally. It may have breaking changes in the future.
8282 #[ inline]
8383 #[ doc( hidden) ]
84- pub fn into_parts ( self ) -> ( ResponseParts , Vec < u8 > ) {
84+ pub fn into_parts ( self ) -> ( ResponseParts , Cow < ' static , [ u8 ] > ) {
8585 ( self . head , self . body )
8686 }
8787
@@ -129,21 +129,21 @@ impl Response {
129129
130130 /// Returns a mutable reference to the associated HTTP body.
131131 #[ inline]
132- pub fn body_mut ( & mut self ) -> & mut Vec < u8 > {
132+ pub fn body_mut ( & mut self ) -> & mut Cow < ' static , [ u8 ] > {
133133 & mut self . body
134134 }
135135
136136 /// Returns a reference to the associated HTTP body.
137137 #[ inline]
138- pub fn body ( & self ) -> & Vec < u8 > {
138+ pub fn body ( & self ) -> & Cow < ' static , [ u8 ] > {
139139 & self . body
140140 }
141141}
142142
143143impl Default for Response {
144144 #[ inline]
145145 fn default ( ) -> Response {
146- Response :: new ( Vec :: new ( ) )
146+ Response :: new ( Default :: default ( ) )
147147 }
148148}
149149
@@ -280,8 +280,11 @@ impl Builder {
280280 /// .body(Vec::new())
281281 /// .unwrap();
282282 /// ```
283- pub fn body ( self , body : Vec < u8 > ) -> Result < Response > {
284- self . inner . map ( move |head| Response { head, body } )
283+ pub fn body ( self , body : impl Into < Cow < ' static , [ u8 ] > > ) -> Result < Response > {
284+ self . inner . map ( move |head| Response {
285+ head,
286+ body : body. into ( ) ,
287+ } )
285288 }
286289
287290 // private
0 commit comments