@@ -56,7 +56,7 @@ pin_project_lite::pin_project! {
5656 pub struct Body {
5757 #[ pin]
5858 reader: Box <dyn AsyncBufRead + Unpin + Send + Sync + ' static >,
59- mime: Mime ,
59+ mime: Option < Mime > ,
6060 length: Option <u64 >,
6161 bytes_read: u64 ,
6262 }
@@ -79,7 +79,7 @@ impl Body {
7979 pub fn empty ( ) -> Self {
8080 Self {
8181 reader : Box :: new ( io:: empty ( ) ) ,
82- mime : mime:: BYTE_STREAM ,
82+ mime : Some ( mime:: BYTE_STREAM ) ,
8383 length : Some ( 0 ) ,
8484 bytes_read : 0 ,
8585 }
@@ -110,7 +110,7 @@ impl Body {
110110 ) -> Self {
111111 Self {
112112 reader : Box :: new ( reader) ,
113- mime : mime:: BYTE_STREAM ,
113+ mime : Some ( mime:: BYTE_STREAM ) ,
114114 length,
115115 bytes_read : 0 ,
116116 }
@@ -153,7 +153,7 @@ impl Body {
153153 /// ```
154154 pub fn from_bytes ( bytes : Vec < u8 > ) -> Self {
155155 Self {
156- mime : mime:: BYTE_STREAM ,
156+ mime : Some ( mime:: BYTE_STREAM ) ,
157157 length : Some ( bytes. len ( ) as u64 ) ,
158158 reader : Box :: new ( io:: Cursor :: new ( bytes) ) ,
159159 bytes_read : 0 ,
@@ -203,7 +203,7 @@ impl Body {
203203 /// ```
204204 pub fn from_string ( s : String ) -> Self {
205205 Self {
206- mime : mime:: PLAIN ,
206+ mime : Some ( mime:: PLAIN ) ,
207207 length : Some ( s. len ( ) as u64 ) ,
208208 reader : Box :: new ( io:: Cursor :: new ( s. into_bytes ( ) ) ) ,
209209 bytes_read : 0 ,
@@ -253,7 +253,7 @@ impl Body {
253253 let body = Self {
254254 length : Some ( bytes. len ( ) as u64 ) ,
255255 reader : Box :: new ( io:: Cursor :: new ( bytes) ) ,
256- mime : mime:: JSON ,
256+ mime : Some ( mime:: JSON ) ,
257257 bytes_read : 0 ,
258258 } ;
259259 Ok ( body)
@@ -322,7 +322,7 @@ impl Body {
322322 let body = Self {
323323 length : Some ( bytes. len ( ) as u64 ) ,
324324 reader : Box :: new ( io:: Cursor :: new ( bytes) ) ,
325- mime : mime:: FORM ,
325+ mime : Some ( mime:: FORM ) ,
326326 bytes_read : 0 ,
327327 } ;
328328 Ok ( body)
@@ -444,7 +444,7 @@ impl Body {
444444 . unwrap_or ( mime:: BYTE_STREAM ) ;
445445
446446 Ok ( Self {
447- mime,
447+ mime : Some ( mime ) ,
448448 length : Some ( len) ,
449449 reader : Box :: new ( io:: BufReader :: new ( file) ) ,
450450 bytes_read : 0 ,
@@ -474,13 +474,26 @@ impl Body {
474474 }
475475
476476 /// Returns the mime type of this Body.
477- pub fn mime ( & self ) -> & Mime {
478- & self . mime
477+ pub fn mime ( & self ) -> Option < & Mime > {
478+ self . mime . as_ref ( )
479479 }
480480
481481 /// Sets the mime type of this Body.
482- pub fn set_mime ( & mut self , mime : impl Into < Mime > ) {
483- self . mime = mime. into ( ) ;
482+ ///
483+ /// # Examples
484+ /// ```
485+ /// use http_types::Body;
486+ /// use http_types::mime;
487+ ///
488+ /// let mut body = Body::empty();
489+ /// body.set_mime(Some(mime::CSS));
490+ /// assert_eq!(body.mime(), Some(&mime::CSS));
491+ ///
492+ /// body.set_mime(None);
493+ /// assert_eq!(body.mime(), None);
494+ /// ```
495+ pub fn set_mime ( & mut self , mime : Option < Mime > ) {
496+ self . mime = mime;
484497 }
485498
486499 /// Create a Body by chaining another Body after this one, consuming both.
@@ -508,7 +521,7 @@ impl Body {
508521 let mime = if self . mime == other. mime {
509522 self . mime . clone ( )
510523 } else {
511- mime:: BYTE_STREAM
524+ Some ( mime:: BYTE_STREAM )
512525 } ;
513526 let length = match ( self . length , other. length ) {
514527 ( Some ( l1) , Some ( l2) ) => ( l1 - self . bytes_read ) . checked_add ( l2 - other. bytes_read ) ,
@@ -728,7 +741,7 @@ mod test {
728741 for buf_len in 1 ..13 {
729742 let mut body = Body :: from ( "hello " ) . chain ( Body :: from ( "world" ) ) ;
730743 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
731- assert_eq ! ( body. mime( ) , & mime:: PLAIN ) ;
744+ assert_eq ! ( body. mime( ) , Some ( & mime:: PLAIN ) ) ;
732745 assert_eq ! (
733746 read_with_buffers_of_size( & mut body, buf_len) . await ?,
734747 "hello world"
@@ -744,7 +757,7 @@ mod test {
744757 for buf_len in 1 ..13 {
745758 let mut body = Body :: from ( & b"hello " [ ..] ) . chain ( Body :: from ( "world" ) ) ;
746759 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
747- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
760+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
748761 assert_eq ! (
749762 read_with_buffers_of_size( & mut body, buf_len) . await ?,
750763 "hello world"
@@ -761,7 +774,7 @@ mod test {
761774 let mut body =
762775 Body :: from_reader ( Cursor :: new ( "hello " ) , Some ( 6 ) ) . chain ( Body :: from ( "world" ) ) ;
763776 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
764- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
777+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
765778 assert_eq ! (
766779 read_with_buffers_of_size( & mut body, buf_len) . await ?,
767780 "hello world"
@@ -778,7 +791,7 @@ mod test {
778791 let mut body =
779792 Body :: from_reader ( Cursor :: new ( "hello " ) , None ) . chain ( Body :: from ( "world" ) ) ;
780793 assert_eq ! ( body. len( ) , None ) ;
781- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
794+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
782795 assert_eq ! (
783796 read_with_buffers_of_size( & mut body, buf_len) . await ?,
784797 "hello world"
@@ -795,7 +808,7 @@ mod test {
795808 let mut body =
796809 Body :: from ( "hello " ) . chain ( Body :: from_reader ( Cursor :: new ( "world" ) , None ) ) ;
797810 assert_eq ! ( body. len( ) , None ) ;
798- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
811+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
799812 assert_eq ! (
800813 read_with_buffers_of_size( & mut body, buf_len) . await ?,
801814 "hello world"
@@ -812,7 +825,7 @@ mod test {
812825 let mut body = Body :: from_reader ( Cursor :: new ( "hello xyz" ) , Some ( 6 ) )
813826 . chain ( Body :: from_reader ( Cursor :: new ( "world abc" ) , Some ( 5 ) ) ) ;
814827 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
815- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
828+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
816829 assert_eq ! (
817830 read_with_buffers_of_size( & mut body, buf_len) . await ?,
818831 "hello world"
@@ -830,7 +843,7 @@ mod test {
830843 . chain ( Body :: from ( & b" " [ ..] ) )
831844 . chain ( Body :: from ( "world" ) ) ;
832845 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
833- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
846+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
834847 assert_eq ! (
835848 read_with_buffers_of_size( & mut body, buf_len) . await ?,
836849 "hello world"
@@ -856,7 +869,7 @@ mod test {
856869
857870 let mut body = body1. chain ( body2) ;
858871 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
859- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
872+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
860873 assert_eq ! (
861874 read_with_buffers_of_size( & mut body, buf_len) . await ?,
862875 "hello world"
0 commit comments