@@ -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,41 @@ 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+ ///
483+ /// # Examples
484+ /// ```
485+ /// use http_types::Body;
486+ /// use http_types::mime::self;
487+ ///
488+ /// let mut body = Body::default();
489+ /// body.set_mime(Some(mime::CSS));
490+ /// assert_eq!(*body.mime(), mime::CSS);
491+ /// ```
482492 pub fn set_mime ( & mut self , mime : impl Into < Mime > ) {
483- self . mime = mime. into ( ) ;
493+ self . mime = Some ( mime. into ( ) ) ;
494+ }
495+
496+ /// Unsets the mime type of this Body.
497+ ///
498+ /// # Examples
499+ /// ```
500+ /// use http_types::Body;
501+ /// use http_types::mime;
502+ ///
503+ /// let mut body = Body::default();
504+ /// assert!(body.mime().is_some());
505+ ///
506+ /// body.unset_mime();
507+ /// assert!(body.mime().is_none());
508+ /// ```
509+ ///
510+ pub fn unset_mime ( & mut self ) {
511+ self . mime = None
484512 }
485513
486514 /// Create a Body by chaining another Body after this one, consuming both.
@@ -508,7 +536,7 @@ impl Body {
508536 let mime = if self . mime == other. mime {
509537 self . mime . clone ( )
510538 } else {
511- mime:: BYTE_STREAM
539+ Some ( mime:: BYTE_STREAM )
512540 } ;
513541 let length = match ( self . length , other. length ) {
514542 ( Some ( l1) , Some ( l2) ) => ( l1 - self . bytes_read ) . checked_add ( l2 - other. bytes_read ) ,
@@ -728,7 +756,7 @@ mod test {
728756 for buf_len in 1 ..13 {
729757 let mut body = Body :: from ( "hello " ) . chain ( Body :: from ( "world" ) ) ;
730758 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
731- assert_eq ! ( body. mime( ) , & mime:: PLAIN ) ;
759+ assert_eq ! ( body. mime( ) , Some ( & mime:: PLAIN ) ) ;
732760 assert_eq ! (
733761 read_with_buffers_of_size( & mut body, buf_len) . await ?,
734762 "hello world"
@@ -744,7 +772,7 @@ mod test {
744772 for buf_len in 1 ..13 {
745773 let mut body = Body :: from ( & b"hello " [ ..] ) . chain ( Body :: from ( "world" ) ) ;
746774 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
747- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
775+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
748776 assert_eq ! (
749777 read_with_buffers_of_size( & mut body, buf_len) . await ?,
750778 "hello world"
@@ -761,7 +789,7 @@ mod test {
761789 let mut body =
762790 Body :: from_reader ( Cursor :: new ( "hello " ) , Some ( 6 ) ) . chain ( Body :: from ( "world" ) ) ;
763791 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
764- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
792+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
765793 assert_eq ! (
766794 read_with_buffers_of_size( & mut body, buf_len) . await ?,
767795 "hello world"
@@ -778,7 +806,7 @@ mod test {
778806 let mut body =
779807 Body :: from_reader ( Cursor :: new ( "hello " ) , None ) . chain ( Body :: from ( "world" ) ) ;
780808 assert_eq ! ( body. len( ) , None ) ;
781- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
809+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
782810 assert_eq ! (
783811 read_with_buffers_of_size( & mut body, buf_len) . await ?,
784812 "hello world"
@@ -795,7 +823,7 @@ mod test {
795823 let mut body =
796824 Body :: from ( "hello " ) . chain ( Body :: from_reader ( Cursor :: new ( "world" ) , None ) ) ;
797825 assert_eq ! ( body. len( ) , None ) ;
798- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
826+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
799827 assert_eq ! (
800828 read_with_buffers_of_size( & mut body, buf_len) . await ?,
801829 "hello world"
@@ -812,7 +840,7 @@ mod test {
812840 let mut body = Body :: from_reader ( Cursor :: new ( "hello xyz" ) , Some ( 6 ) )
813841 . chain ( Body :: from_reader ( Cursor :: new ( "world abc" ) , Some ( 5 ) ) ) ;
814842 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
815- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
843+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
816844 assert_eq ! (
817845 read_with_buffers_of_size( & mut body, buf_len) . await ?,
818846 "hello world"
@@ -830,7 +858,7 @@ mod test {
830858 . chain ( Body :: from ( & b" " [ ..] ) )
831859 . chain ( Body :: from ( "world" ) ) ;
832860 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
833- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
861+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
834862 assert_eq ! (
835863 read_with_buffers_of_size( & mut body, buf_len) . await ?,
836864 "hello world"
@@ -856,7 +884,7 @@ mod test {
856884
857885 let mut body = body1. chain ( body2) ;
858886 assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
859- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
887+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
860888 assert_eq ! (
861889 read_with_buffers_of_size( & mut body, buf_len) . await ?,
862890 "hello world"
0 commit comments