Skip to content

Commit 871873c

Browse files
tottotoseanmonstar
authored andcommitted
chore(lib): refactor conditional expression
1 parent 6e3042a commit 871873c

File tree

5 files changed

+41
-63
lines changed

5 files changed

+41
-63
lines changed

src/body/incoming.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,9 @@ impl Body for Incoming {
186186
want_tx.send(WANT_READY);
187187

188188
if !data_rx.is_terminated() {
189-
match ready!(Pin::new(data_rx).poll_next(cx)?) {
190-
Some(chunk) => {
191-
len.sub_if(chunk.len() as u64);
192-
return Poll::Ready(Some(Ok(Frame::data(chunk))));
193-
}
194-
// fall through to trailers
195-
None => (),
189+
if let Some(chunk) = ready!(Pin::new(data_rx).poll_next(cx)?) {
190+
len.sub_if(chunk.len() as u64);
191+
return Poll::Ready(Some(Ok(Frame::data(chunk))));
196192
}
197193
}
198194

src/headers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ fn from_digits(bytes: &[u8]) -> Option<u64> {
9898

9999
#[cfg(all(feature = "http2", feature = "client"))]
100100
pub(super) fn method_has_defined_payload_semantics(method: &Method) -> bool {
101-
match *method {
102-
Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT => false,
103-
_ => true,
104-
}
101+
!matches!(
102+
*method,
103+
Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT
104+
)
105105
}
106106

107107
#[cfg(feature = "http2")]

src/proto/h1/conn.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ where
169169
}
170170

171171
pub(crate) fn can_read_body(&self) -> bool {
172-
match self.state.reading {
173-
Reading::Body(..) | Reading::Continue(..) => true,
174-
_ => false,
175-
}
172+
matches!(
173+
self.state.reading,
174+
Reading::Body(..) | Reading::Continue(..)
175+
)
176176
}
177177

178178
#[cfg(feature = "server")]
@@ -950,11 +950,7 @@ impl State {
950950
}
951951

952952
fn wants_keep_alive(&self) -> bool {
953-
if let KA::Disabled = self.keep_alive.status() {
954-
false
955-
} else {
956-
true
957-
}
953+
!matches!(self.keep_alive.status(), KA::Disabled)
958954
}
959955

960956
fn try_keep_alive<T: Http1Transaction>(&mut self) {

src/proto/h1/role.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -471,27 +471,21 @@ impl Server {
471471
}
472472

473473
fn can_chunked(method: &Option<Method>, status: StatusCode) -> bool {
474-
if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success()
474+
if method == &Some(Method::HEAD)
475+
|| method == &Some(Method::CONNECT) && status.is_success()
476+
|| status.is_informational()
475477
{
476478
false
477-
} else if status.is_informational() {
478-
false
479479
} else {
480-
match status {
481-
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
482-
_ => true,
483-
}
480+
!matches!(status, StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED)
484481
}
485482
}
486483

487484
fn can_have_content_length(method: &Option<Method>, status: StatusCode) -> bool {
488485
if status.is_informational() || method == &Some(Method::CONNECT) && status.is_success() {
489486
false
490487
} else {
491-
match status {
492-
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
493-
_ => true,
494-
}
488+
!matches!(status, StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED)
495489
}
496490
}
497491

src/proto/h2/client.rs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,18 @@ where
307307
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
308308
let mut this = self.project();
309309

310-
if !this.conn.is_terminated() {
311-
if let Poll::Ready(_) = this.conn.poll_unpin(cx) {
312-
// ok or err, the `conn` has finished.
313-
return Poll::Ready(());
314-
};
310+
if !this.conn.is_terminated() && this.conn.poll_unpin(cx).is_ready() {
311+
// ok or err, the `conn` has finished.
312+
return Poll::Ready(());
315313
}
316314

317-
if !this.drop_rx.is_terminated() {
318-
if let Poll::Ready(_) = this.drop_rx.poll_unpin(cx) {
319-
// mpsc has been dropped, hopefully polling
320-
// the connection some more should start shutdown
321-
// and then close.
322-
trace!("send_request dropped, starting conn shutdown");
323-
drop(this.cancel_tx.take().expect("ConnTask Future polled twice"));
324-
}
325-
};
315+
if !this.drop_rx.is_terminated() && this.drop_rx.poll_unpin(cx).is_ready() {
316+
// mpsc has been dropped, hopefully polling
317+
// the connection some more should start shutdown
318+
// and then close.
319+
trace!("send_request dropped, starting conn shutdown");
320+
drop(this.cancel_tx.take().expect("ConnTask Future polled twice"));
321+
}
326322

327323
Poll::Pending
328324
}
@@ -613,14 +609,11 @@ where
613609
}
614610
};
615611

616-
match self.fut_ctx.take() {
617-
// If we were waiting on pending open
618-
// continue where we left off.
619-
Some(f) => {
620-
self.poll_pipe(f, cx);
621-
continue;
622-
}
623-
None => (),
612+
// If we were waiting on pending open
613+
// continue where we left off.
614+
if let Some(f) = self.fut_ctx.take() {
615+
self.poll_pipe(f, cx);
616+
continue;
624617
}
625618

626619
match self.req_rx.poll_recv(cx) {
@@ -642,17 +635,16 @@ where
642635
let is_connect = req.method() == Method::CONNECT;
643636
let eos = body.is_end_stream();
644637

645-
if is_connect {
646-
if headers::content_length_parse_all(req.headers())
638+
if is_connect
639+
&& headers::content_length_parse_all(req.headers())
647640
.map_or(false, |len| len != 0)
648-
{
649-
warn!("h2 connect request with non-zero body not supported");
650-
cb.send(Err((
651-
crate::Error::new_h2(h2::Reason::INTERNAL_ERROR.into()),
652-
None,
653-
)));
654-
continue;
655-
}
641+
{
642+
warn!("h2 connect request with non-zero body not supported");
643+
cb.send(Err((
644+
crate::Error::new_h2(h2::Reason::INTERNAL_ERROR.into()),
645+
None,
646+
)));
647+
continue;
656648
}
657649

658650
if let Some(protocol) = req.extensions_mut().remove::<Protocol>() {

0 commit comments

Comments
 (0)