Skip to content

Commit faf24c6

Browse files
authored
refactor(http1): assorted code readability improvements in h1/conn.rs (#2817)
* refactor: use `matches` macro to flatten code * refactor: prefer `matches` over explicit matching * refactor: reuse `can_write_body` method * refactor: use `matches` over `if`-`let` * refactor: nested `if`-`else` as early return * refactor: move inner `match` logic outside * refactor: unneeded `return` in `match` * refactor: remove unneeded reference matching * refactor: use early returns for idle check * refactor: use `matches` macro for Boolean `match`
1 parent 6a35c17 commit faf24c6

File tree

1 file changed

+63
-80
lines changed

1 file changed

+63
-80
lines changed

src/proto/h1/conn.rs

+63-80
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,15 @@ where
155155
}
156156

157157
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;
170164
}
165+
166+
!matches!(self.state.writing, Writing::Init)
171167
}
172168

173169
pub(crate) fn can_read_body(&self) -> bool {
@@ -367,10 +363,10 @@ where
367363
}
368364

369365
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+
)
374370
}
375371

376372
// This will check to make sure the io object read is empty.
@@ -493,11 +489,10 @@ where
493489
}
494490

495491
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;
500494
}
495+
501496
match self.state.writing {
502497
Writing::Init => self.io.can_headers_buf(),
503498
_ => false,
@@ -641,15 +636,15 @@ where
641636
Writing::Body(ref mut encoder) => {
642637
self.io.buffer(encoder.encode(chunk));
643638

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() {
651640
return;
652641
}
642+
643+
if encoder.is_last() {
644+
Writing::Closed
645+
} else {
646+
Writing::KeepAlive
647+
}
653648
}
654649
_ => unreachable!("write_body invalid state: {:?}", self.state.writing),
655650
};
@@ -680,32 +675,31 @@ where
680675
pub(crate) fn end_body(&mut self) -> crate::Result<()> {
681676
debug_assert!(self.can_write_body());
682677

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,
704680
_ => return Ok(()),
705681
};
706682

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+
}
709703
}
710704

711705
// When we get a parse error, depending on what side we are, we might be able
@@ -758,10 +752,7 @@ where
758752

759753
// If still in Reading::Body, just give up
760754
match self.state.reading {
761-
Reading::Init | Reading::KeepAlive => {
762-
trace!("body drained");
763-
return;
764-
}
755+
Reading::Init | Reading::KeepAlive => trace!("body drained"),
765756
_ => self.close_read(),
766757
}
767758
}
@@ -1012,43 +1003,35 @@ impl State {
10121003

10131004
self.method = None;
10141005
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() {
10281008
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;
10291022
}
10301023
}
10311024

10321025
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)
10381027
}
10391028

10401029
fn is_read_closed(&self) -> bool {
1041-
match self.reading {
1042-
Reading::Closed => true,
1043-
_ => false,
1044-
}
1030+
matches!(self.reading, Reading::Closed)
10451031
}
10461032

10471033
fn is_write_closed(&self) -> bool {
1048-
match self.writing {
1049-
Writing::Closed => true,
1050-
_ => false,
1051-
}
1034+
matches!(self.writing, Writing::Closed)
10521035
}
10531036

10541037
fn prepare_upgrade(&mut self) -> crate::upgrade::OnUpgrade {

0 commit comments

Comments
 (0)