@@ -56,7 +56,7 @@ pin_project_lite::pin_project! {
56
56
pub struct Body {
57
57
#[ pin]
58
58
reader: Box <dyn AsyncBufRead + Unpin + Send + Sync + ' static >,
59
- mime: Mime ,
59
+ mime: Option < Mime > ,
60
60
length: Option <u64 >,
61
61
bytes_read: u64 ,
62
62
}
@@ -79,7 +79,7 @@ impl Body {
79
79
pub fn empty ( ) -> Self {
80
80
Self {
81
81
reader : Box :: new ( io:: empty ( ) ) ,
82
- mime : mime:: BYTE_STREAM ,
82
+ mime : Some ( mime:: BYTE_STREAM ) ,
83
83
length : Some ( 0 ) ,
84
84
bytes_read : 0 ,
85
85
}
@@ -110,7 +110,7 @@ impl Body {
110
110
) -> Self {
111
111
Self {
112
112
reader : Box :: new ( reader) ,
113
- mime : mime:: BYTE_STREAM ,
113
+ mime : Some ( mime:: BYTE_STREAM ) ,
114
114
length,
115
115
bytes_read : 0 ,
116
116
}
@@ -153,7 +153,7 @@ impl Body {
153
153
/// ```
154
154
pub fn from_bytes ( bytes : Vec < u8 > ) -> Self {
155
155
Self {
156
- mime : mime:: BYTE_STREAM ,
156
+ mime : Some ( mime:: BYTE_STREAM ) ,
157
157
length : Some ( bytes. len ( ) as u64 ) ,
158
158
reader : Box :: new ( io:: Cursor :: new ( bytes) ) ,
159
159
bytes_read : 0 ,
@@ -203,7 +203,7 @@ impl Body {
203
203
/// ```
204
204
pub fn from_string ( s : String ) -> Self {
205
205
Self {
206
- mime : mime:: PLAIN ,
206
+ mime : Some ( mime:: PLAIN ) ,
207
207
length : Some ( s. len ( ) as u64 ) ,
208
208
reader : Box :: new ( io:: Cursor :: new ( s. into_bytes ( ) ) ) ,
209
209
bytes_read : 0 ,
@@ -253,7 +253,7 @@ impl Body {
253
253
let body = Self {
254
254
length : Some ( bytes. len ( ) as u64 ) ,
255
255
reader : Box :: new ( io:: Cursor :: new ( bytes) ) ,
256
- mime : mime:: JSON ,
256
+ mime : Some ( mime:: JSON ) ,
257
257
bytes_read : 0 ,
258
258
} ;
259
259
Ok ( body)
@@ -322,7 +322,7 @@ impl Body {
322
322
let body = Self {
323
323
length : Some ( bytes. len ( ) as u64 ) ,
324
324
reader : Box :: new ( io:: Cursor :: new ( bytes) ) ,
325
- mime : mime:: FORM ,
325
+ mime : Some ( mime:: FORM ) ,
326
326
bytes_read : 0 ,
327
327
} ;
328
328
Ok ( body)
@@ -444,7 +444,7 @@ impl Body {
444
444
. unwrap_or ( mime:: BYTE_STREAM ) ;
445
445
446
446
Ok ( Self {
447
- mime,
447
+ mime : Some ( mime ) ,
448
448
length : Some ( len) ,
449
449
reader : Box :: new ( io:: BufReader :: new ( file) ) ,
450
450
bytes_read : 0 ,
@@ -474,13 +474,26 @@ impl Body {
474
474
}
475
475
476
476
/// 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 ( )
479
479
}
480
480
481
481
/// 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;
484
497
}
485
498
486
499
/// Create a Body by chaining another Body after this one, consuming both.
@@ -508,7 +521,7 @@ impl Body {
508
521
let mime = if self . mime == other. mime {
509
522
self . mime . clone ( )
510
523
} else {
511
- mime:: BYTE_STREAM
524
+ Some ( mime:: BYTE_STREAM )
512
525
} ;
513
526
let length = match ( self . length , other. length ) {
514
527
( Some ( l1) , Some ( l2) ) => ( l1 - self . bytes_read ) . checked_add ( l2 - other. bytes_read ) ,
@@ -728,7 +741,7 @@ mod test {
728
741
for buf_len in 1 ..13 {
729
742
let mut body = Body :: from ( "hello " ) . chain ( Body :: from ( "world" ) ) ;
730
743
assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
731
- assert_eq ! ( body. mime( ) , & mime:: PLAIN ) ;
744
+ assert_eq ! ( body. mime( ) , Some ( & mime:: PLAIN ) ) ;
732
745
assert_eq ! (
733
746
read_with_buffers_of_size( & mut body, buf_len) . await ?,
734
747
"hello world"
@@ -744,7 +757,7 @@ mod test {
744
757
for buf_len in 1 ..13 {
745
758
let mut body = Body :: from ( & b"hello " [ ..] ) . chain ( Body :: from ( "world" ) ) ;
746
759
assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
747
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
760
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
748
761
assert_eq ! (
749
762
read_with_buffers_of_size( & mut body, buf_len) . await ?,
750
763
"hello world"
@@ -761,7 +774,7 @@ mod test {
761
774
let mut body =
762
775
Body :: from_reader ( Cursor :: new ( "hello " ) , Some ( 6 ) ) . chain ( Body :: from ( "world" ) ) ;
763
776
assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
764
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
777
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
765
778
assert_eq ! (
766
779
read_with_buffers_of_size( & mut body, buf_len) . await ?,
767
780
"hello world"
@@ -778,7 +791,7 @@ mod test {
778
791
let mut body =
779
792
Body :: from_reader ( Cursor :: new ( "hello " ) , None ) . chain ( Body :: from ( "world" ) ) ;
780
793
assert_eq ! ( body. len( ) , None ) ;
781
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
794
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
782
795
assert_eq ! (
783
796
read_with_buffers_of_size( & mut body, buf_len) . await ?,
784
797
"hello world"
@@ -795,7 +808,7 @@ mod test {
795
808
let mut body =
796
809
Body :: from ( "hello " ) . chain ( Body :: from_reader ( Cursor :: new ( "world" ) , None ) ) ;
797
810
assert_eq ! ( body. len( ) , None ) ;
798
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
811
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
799
812
assert_eq ! (
800
813
read_with_buffers_of_size( & mut body, buf_len) . await ?,
801
814
"hello world"
@@ -812,7 +825,7 @@ mod test {
812
825
let mut body = Body :: from_reader ( Cursor :: new ( "hello xyz" ) , Some ( 6 ) )
813
826
. chain ( Body :: from_reader ( Cursor :: new ( "world abc" ) , Some ( 5 ) ) ) ;
814
827
assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
815
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
828
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
816
829
assert_eq ! (
817
830
read_with_buffers_of_size( & mut body, buf_len) . await ?,
818
831
"hello world"
@@ -830,7 +843,7 @@ mod test {
830
843
. chain ( Body :: from ( & b" " [ ..] ) )
831
844
. chain ( Body :: from ( "world" ) ) ;
832
845
assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
833
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
846
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
834
847
assert_eq ! (
835
848
read_with_buffers_of_size( & mut body, buf_len) . await ?,
836
849
"hello world"
@@ -856,7 +869,7 @@ mod test {
856
869
857
870
let mut body = body1. chain ( body2) ;
858
871
assert_eq ! ( body. len( ) , Some ( 11 ) ) ;
859
- assert_eq ! ( body. mime( ) , & mime:: BYTE_STREAM ) ;
872
+ assert_eq ! ( body. mime( ) , Some ( & mime:: BYTE_STREAM ) ) ;
860
873
assert_eq ! (
861
874
read_with_buffers_of_size( & mut body, buf_len) . await ?,
862
875
"hello world"
0 commit comments