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`
This commit is contained in:
@@ -155,19 +155,15 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn can_read_head(&self) -> bool {
|
pub(crate) fn can_read_head(&self) -> bool {
|
||||||
match self.state.reading {
|
if !matches!(self.state.reading, Reading::Init) {
|
||||||
Reading::Init => {
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if T::should_read_first() {
|
if T::should_read_first() {
|
||||||
true
|
return true;
|
||||||
} else {
|
|
||||||
match self.state.writing {
|
|
||||||
Writing::Init => false,
|
|
||||||
_ => true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
!matches!(self.state.writing, Writing::Init)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn can_read_body(&self) -> bool {
|
pub(crate) fn can_read_body(&self) -> bool {
|
||||||
@@ -367,10 +363,10 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_mid_message(&self) -> bool {
|
fn is_mid_message(&self) -> bool {
|
||||||
match (&self.state.reading, &self.state.writing) {
|
!matches!(
|
||||||
(&Reading::Init, &Writing::Init) => false,
|
(&self.state.reading, &self.state.writing),
|
||||||
_ => true,
|
(&Reading::Init, &Writing::Init)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will check to make sure the io object read is empty.
|
// This will check to make sure the io object read is empty.
|
||||||
@@ -493,11 +489,10 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn can_write_head(&self) -> bool {
|
pub(crate) fn can_write_head(&self) -> bool {
|
||||||
if !T::should_read_first() {
|
if !T::should_read_first() && matches!(self.state.reading, Reading::Closed) {
|
||||||
if let Reading::Closed = self.state.reading {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
match self.state.writing {
|
match self.state.writing {
|
||||||
Writing::Init => self.io.can_headers_buf(),
|
Writing::Init => self.io.can_headers_buf(),
|
||||||
_ => false,
|
_ => false,
|
||||||
@@ -641,15 +636,15 @@ where
|
|||||||
Writing::Body(ref mut encoder) => {
|
Writing::Body(ref mut encoder) => {
|
||||||
self.io.buffer(encoder.encode(chunk));
|
self.io.buffer(encoder.encode(chunk));
|
||||||
|
|
||||||
if encoder.is_eof() {
|
if !encoder.is_eof() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if encoder.is_last() {
|
if encoder.is_last() {
|
||||||
Writing::Closed
|
Writing::Closed
|
||||||
} else {
|
} else {
|
||||||
Writing::KeepAlive
|
Writing::KeepAlive
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => unreachable!("write_body invalid state: {:?}", self.state.writing),
|
_ => unreachable!("write_body invalid state: {:?}", self.state.writing),
|
||||||
};
|
};
|
||||||
@@ -680,32 +675,31 @@ where
|
|||||||
pub(crate) fn end_body(&mut self) -> crate::Result<()> {
|
pub(crate) fn end_body(&mut self) -> crate::Result<()> {
|
||||||
debug_assert!(self.can_write_body());
|
debug_assert!(self.can_write_body());
|
||||||
|
|
||||||
let mut res = Ok(());
|
let encoder = match self.state.writing {
|
||||||
let state = match self.state.writing {
|
Writing::Body(ref mut enc) => enc,
|
||||||
Writing::Body(ref mut encoder) => {
|
_ => return Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
// end of stream, that means we should try to eof
|
// end of stream, that means we should try to eof
|
||||||
match encoder.end() {
|
match encoder.end() {
|
||||||
Ok(end) => {
|
Ok(end) => {
|
||||||
if let Some(end) = end {
|
if let Some(end) = end {
|
||||||
self.io.buffer(end);
|
self.io.buffer(end);
|
||||||
}
|
}
|
||||||
if encoder.is_last() || encoder.is_close_delimited() {
|
|
||||||
|
self.state.writing = if encoder.is_last() || encoder.is_close_delimited() {
|
||||||
Writing::Closed
|
Writing::Closed
|
||||||
} else {
|
} else {
|
||||||
Writing::KeepAlive
|
Writing::KeepAlive
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(not_eof) => {
|
|
||||||
res = Err(crate::Error::new_body_write_aborted().with(not_eof));
|
|
||||||
Writing::Closed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => return Ok(()),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.state.writing = state;
|
Ok(())
|
||||||
res
|
}
|
||||||
|
Err(not_eof) => {
|
||||||
|
self.state.writing = Writing::Closed;
|
||||||
|
Err(crate::Error::new_body_write_aborted().with(not_eof))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When we get a parse error, depending on what side we are, we might be able
|
// When we get a parse error, depending on what side we are, we might be able
|
||||||
@@ -758,10 +752,7 @@ where
|
|||||||
|
|
||||||
// If still in Reading::Body, just give up
|
// If still in Reading::Body, just give up
|
||||||
match self.state.reading {
|
match self.state.reading {
|
||||||
Reading::Init | Reading::KeepAlive => {
|
Reading::Init | Reading::KeepAlive => trace!("body drained"),
|
||||||
trace!("body drained");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_ => self.close_read(),
|
_ => self.close_read(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1012,7 +1003,12 @@ impl State {
|
|||||||
|
|
||||||
self.method = None;
|
self.method = None;
|
||||||
self.keep_alive.idle();
|
self.keep_alive.idle();
|
||||||
if self.is_idle() {
|
|
||||||
|
if !self.is_idle() {
|
||||||
|
self.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
self.reading = Reading::Init;
|
self.reading = Reading::Init;
|
||||||
self.writing = Writing::Init;
|
self.writing = Writing::Init;
|
||||||
|
|
||||||
@@ -1024,31 +1020,18 @@ impl State {
|
|||||||
if !T::should_read_first() {
|
if !T::should_read_first() {
|
||||||
self.notify_read = true;
|
self.notify_read = true;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
self.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_idle(&self) -> bool {
|
fn is_idle(&self) -> bool {
|
||||||
if let KA::Idle = self.keep_alive.status() {
|
matches!(self.keep_alive.status(), KA::Idle)
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_read_closed(&self) -> bool {
|
fn is_read_closed(&self) -> bool {
|
||||||
match self.reading {
|
matches!(self.reading, Reading::Closed)
|
||||||
Reading::Closed => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_write_closed(&self) -> bool {
|
fn is_write_closed(&self) -> bool {
|
||||||
match self.writing {
|
matches!(self.writing, Writing::Closed)
|
||||||
Writing::Closed => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_upgrade(&mut self) -> crate::upgrade::OnUpgrade {
|
fn prepare_upgrade(&mut self) -> crate::upgrade::OnUpgrade {
|
||||||
|
|||||||
Reference in New Issue
Block a user