@@ -155,19 +155,15 @@ where
155
155
}
156
156
157
157
pub ( crate ) fn can_read_head ( & self ) -> bool {
158
- match self . state . reading {
159
- Reading :: Init => {
160
- if T :: should_read_first ( ) {
161
- true
162
- } else {
163
- match self . state . writing {
164
- Writing :: Init => false ,
165
- _ => true ,
166
- }
167
- }
168
- }
169
- _ => false ,
158
+ if !matches ! ( self . state. reading, Reading :: Init ) {
159
+ return false ;
160
+ }
161
+
162
+ if T :: should_read_first ( ) {
163
+ return true ;
170
164
}
165
+
166
+ !matches ! ( self . state. writing, Writing :: Init )
171
167
}
172
168
173
169
pub ( crate ) fn can_read_body ( & self ) -> bool {
@@ -367,10 +363,10 @@ where
367
363
}
368
364
369
365
fn is_mid_message ( & self ) -> bool {
370
- match ( & self . state . reading , & self . state . writing ) {
371
- ( & Reading :: Init , & Writing :: Init ) => false ,
372
- _ => true ,
373
- }
366
+ ! matches ! (
367
+ ( & self . state . reading , & self . state . writing ) ,
368
+ ( & Reading :: Init , & Writing :: Init )
369
+ )
374
370
}
375
371
376
372
// This will check to make sure the io object read is empty.
@@ -493,11 +489,10 @@ where
493
489
}
494
490
495
491
pub ( crate ) fn can_write_head ( & self ) -> bool {
496
- if !T :: should_read_first ( ) {
497
- if let Reading :: Closed = self . state . reading {
498
- return false ;
499
- }
492
+ if !T :: should_read_first ( ) && matches ! ( self . state. reading, Reading :: Closed ) {
493
+ return false ;
500
494
}
495
+
501
496
match self . state . writing {
502
497
Writing :: Init => self . io . can_headers_buf ( ) ,
503
498
_ => false ,
@@ -641,15 +636,15 @@ where
641
636
Writing :: Body ( ref mut encoder) => {
642
637
self . io . buffer ( encoder. encode ( chunk) ) ;
643
638
644
- if encoder. is_eof ( ) {
645
- if encoder. is_last ( ) {
646
- Writing :: Closed
647
- } else {
648
- Writing :: KeepAlive
649
- }
650
- } else {
639
+ if !encoder. is_eof ( ) {
651
640
return ;
652
641
}
642
+
643
+ if encoder. is_last ( ) {
644
+ Writing :: Closed
645
+ } else {
646
+ Writing :: KeepAlive
647
+ }
653
648
}
654
649
_ => unreachable ! ( "write_body invalid state: {:?}" , self . state. writing) ,
655
650
} ;
@@ -680,32 +675,31 @@ where
680
675
pub ( crate ) fn end_body ( & mut self ) -> crate :: Result < ( ) > {
681
676
debug_assert ! ( self . can_write_body( ) ) ;
682
677
683
- let mut res = Ok ( ( ) ) ;
684
- let state = match self . state . writing {
685
- Writing :: Body ( ref mut encoder) => {
686
- // end of stream, that means we should try to eof
687
- match encoder. end ( ) {
688
- Ok ( end) => {
689
- if let Some ( end) = end {
690
- self . io . buffer ( end) ;
691
- }
692
- if encoder. is_last ( ) || encoder. is_close_delimited ( ) {
693
- Writing :: Closed
694
- } else {
695
- Writing :: KeepAlive
696
- }
697
- }
698
- Err ( not_eof) => {
699
- res = Err ( crate :: Error :: new_body_write_aborted ( ) . with ( not_eof) ) ;
700
- Writing :: Closed
701
- }
702
- }
703
- }
678
+ let encoder = match self . state . writing {
679
+ Writing :: Body ( ref mut enc) => enc,
704
680
_ => return Ok ( ( ) ) ,
705
681
} ;
706
682
707
- self . state . writing = state;
708
- res
683
+ // end of stream, that means we should try to eof
684
+ match encoder. end ( ) {
685
+ Ok ( end) => {
686
+ if let Some ( end) = end {
687
+ self . io . buffer ( end) ;
688
+ }
689
+
690
+ self . state . writing = if encoder. is_last ( ) || encoder. is_close_delimited ( ) {
691
+ Writing :: Closed
692
+ } else {
693
+ Writing :: KeepAlive
694
+ } ;
695
+
696
+ Ok ( ( ) )
697
+ }
698
+ Err ( not_eof) => {
699
+ self . state . writing = Writing :: Closed ;
700
+ Err ( crate :: Error :: new_body_write_aborted ( ) . with ( not_eof) )
701
+ }
702
+ }
709
703
}
710
704
711
705
// When we get a parse error, depending on what side we are, we might be able
@@ -758,10 +752,7 @@ where
758
752
759
753
// If still in Reading::Body, just give up
760
754
match self . state . reading {
761
- Reading :: Init | Reading :: KeepAlive => {
762
- trace ! ( "body drained" ) ;
763
- return ;
764
- }
755
+ Reading :: Init | Reading :: KeepAlive => trace ! ( "body drained" ) ,
765
756
_ => self . close_read ( ) ,
766
757
}
767
758
}
@@ -1012,43 +1003,35 @@ impl State {
1012
1003
1013
1004
self . method = None ;
1014
1005
self . keep_alive . idle ( ) ;
1015
- if self . is_idle ( ) {
1016
- self . reading = Reading :: Init ;
1017
- self . writing = Writing :: Init ;
1018
-
1019
- // !T::should_read_first() means Client.
1020
- //
1021
- // If Client connection has just gone idle, the Dispatcher
1022
- // should try the poll loop one more time, so as to poll the
1023
- // pending requests stream.
1024
- if !T :: should_read_first ( ) {
1025
- self . notify_read = true ;
1026
- }
1027
- } else {
1006
+
1007
+ if !self . is_idle ( ) {
1028
1008
self . close ( ) ;
1009
+ return ;
1010
+ }
1011
+
1012
+ self . reading = Reading :: Init ;
1013
+ self . writing = Writing :: Init ;
1014
+
1015
+ // !T::should_read_first() means Client.
1016
+ //
1017
+ // If Client connection has just gone idle, the Dispatcher
1018
+ // should try the poll loop one more time, so as to poll the
1019
+ // pending requests stream.
1020
+ if !T :: should_read_first ( ) {
1021
+ self . notify_read = true ;
1029
1022
}
1030
1023
}
1031
1024
1032
1025
fn is_idle ( & self ) -> bool {
1033
- if let KA :: Idle = self . keep_alive . status ( ) {
1034
- true
1035
- } else {
1036
- false
1037
- }
1026
+ matches ! ( self . keep_alive. status( ) , KA :: Idle )
1038
1027
}
1039
1028
1040
1029
fn is_read_closed ( & self ) -> bool {
1041
- match self . reading {
1042
- Reading :: Closed => true ,
1043
- _ => false ,
1044
- }
1030
+ matches ! ( self . reading, Reading :: Closed )
1045
1031
}
1046
1032
1047
1033
fn is_write_closed ( & self ) -> bool {
1048
- match self . writing {
1049
- Writing :: Closed => true ,
1050
- _ => false ,
1051
- }
1034
+ matches ! ( self . writing, Writing :: Closed )
1052
1035
}
1053
1036
1054
1037
fn prepare_upgrade ( & mut self ) -> crate :: upgrade:: OnUpgrade {
0 commit comments