Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 22, 2020
1 parent 3cdfdad commit 1e2bc4a
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 94 deletions.
6 changes: 3 additions & 3 deletions ntex-router/src/quoter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub(super) fn requote(val: &[u8]) -> Option<String> {

#[inline]
fn from_hex(v: u8) -> Option<u8> {
if v >= b'0' && v <= b'9' {
if (b'0'..=b'9').contains(&v) {
Some(v - 0x30) // ord('0') == 0x30
} else if v >= b'A' && v <= b'F' {
} else if (b'A'..=b'F').contains(&v) {
Some(v - 0x41 + 10) // ord('A') == 0x41
} else if v >= b'a' && v <= b'f' {
} else if (b'a'..=b'f').contains(&v) {
Some(v - 0x61 + 10) // ord('a') == 0x61
} else {
None
Expand Down
13 changes: 5 additions & 8 deletions ntex-router/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ enum PathElement {

impl PathElement {
fn is_str(&self) -> bool {
match self {
PathElement::Str(_) => true,
_ => false,
}
matches!(self, PathElement::Str(_))
}

fn into_str(self) -> String {
match self {
PathElement::Str(s) => s,
_ => panic!(),
}
}

fn as_str(&self) -> &str {
match self {
PathElement::Str(s) => s.as_str(),
Expand Down Expand Up @@ -380,10 +379,8 @@ impl ResourceDef {
}
if !pattern.is_empty() {
// handle tail expression for static segment
if pattern.ends_with('*') {
let pattern =
Regex::new(&format!("^{}(.+)", &pattern[..pattern.len() - 1]))
.unwrap();
if let Some(stripped) = pattern.strip_suffix('*') {
let pattern = Regex::new(&format!("^{}(.+)", stripped)).unwrap();
pelems.push(Segment::Dynamic {
pattern,
names: Vec::new(),
Expand Down
15 changes: 6 additions & 9 deletions ntex-router/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ impl Tree {
}
}

let path = if path.starts_with('/') {
&path[1..]
let path = if let Some(path) = path.strip_prefix('/') {
path
} else {
base_skip -= 1;
path
Expand Down Expand Up @@ -282,8 +282,8 @@ impl Tree {
return None;
}

let path = if path.starts_with('/') {
&path[1..]
let path = if let Some(path) = path.strip_prefix('/') {
path
} else {
base_skip -= 1;
path
Expand Down Expand Up @@ -356,11 +356,8 @@ impl Tree {
path.len()
};
let segment = T::unquote(&path[..idx]);
let quoted = if let Cow::Owned(_) = segment {
true
} else {
false
};
let quoted = matches!(segment, Cow::Owned(_));

// check segment match
let is_match = match key[0] {
Segment::Static(ref pattern) => {
Expand Down
5 changes: 1 addition & 4 deletions ntex-rt/src/arbiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ impl Arbiter {
.unbounded_send(SystemCommand::RegisterArbiter(id, arb));

// run loop
let _ = match rt.block_on(stop_rx) {
Ok(code) => code,
Err(_) => 1,
};
let _ = rt.block_on(stop_rx);

// unregister arbiter
let _ = System::current()
Expand Down
5 changes: 2 additions & 3 deletions ntex-rt/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl AsyncSystemRunner {

// run loop
lazy(|_| async {
let res = match stop.await {
match stop.await {
Ok(code) => {
if code != 0 {
Err(io::Error::new(
Expand All @@ -142,8 +142,7 @@ impl AsyncSystemRunner {
}
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
};
return res;
}
})
.flatten()
}
Expand Down
15 changes: 3 additions & 12 deletions ntex/src/http/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ pub enum BodySize {

impl BodySize {
pub fn is_eof(&self) -> bool {
match self {
BodySize::None | BodySize::Empty | BodySize::Sized(0) => true,
_ => false,
}
matches!(self, BodySize::None | BodySize::Empty | BodySize::Sized(0))
}
}

Expand Down Expand Up @@ -191,14 +188,8 @@ impl MessageBody for Body {
impl PartialEq for Body {
fn eq(&self, other: &Body) -> bool {
match *self {
Body::None => match *other {
Body::None => true,
_ => false,
},
Body::Empty => match *other {
Body::Empty => true,
_ => false,
},
Body::None => matches!(*other, Body::None),
Body::Empty => matches!(*other, Body::Empty),
Body::Bytes(ref b) => match *other {
Body::Bytes(ref b2) => b == b2,
_ => false,
Expand Down
8 changes: 4 additions & 4 deletions ntex/src/http/client/h2proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ where
trace!("Sending client request: {:?} {:?}", head, body.size());
let head_req = head.as_ref().method == Method::HEAD;
let length = body.size();
let eof = match length {
BodySize::None | BodySize::Empty | BodySize::Sized(0) => true,
_ => false,
};
let eof = matches!(
length,
BodySize::None | BodySize::Empty | BodySize::Sized(0)
);

let mut req = Request::new(());
*req.uri_mut() = head.as_ref().uri.clone();
Expand Down
4 changes: 2 additions & 2 deletions ntex/src/http/client/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ where
// use existing connection
Acquire::Acquired(io, created) => {
trace!("Use existing connection for {:?}", req.uri);
return Ok(IoConnection::new(
Ok(IoConnection::new(
io,
created,
Some(Acquired(key, Some(inner))),
));
))
}
// open new tcp connection
Acquire::Available => {
Expand Down
2 changes: 1 addition & 1 deletion ntex/src/http/client/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl RequestHeadType {
SendClientRequest::new(
config.connector.send_request(self, body.into(), addr),
response_decompress,
timeout.or_else(|| config.timeout),
timeout.or(config.timeout),
)
}

Expand Down
4 changes: 1 addition & 3 deletions ntex/src/http/client/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::Stream;
use crate::codec::{AsyncRead, AsyncWrite, Framed};
use crate::http::error::HttpError;
use crate::http::header::{self, HeaderName, HeaderValue, AUTHORIZATION};
use crate::http::{ConnectionType, Method, StatusCode, Uri, Version};
use crate::http::{ConnectionType, StatusCode, Uri};
use crate::http::{Payload, RequestHead};
use crate::rt::time::timeout;
use crate::service::{IntoService, Service};
Expand Down Expand Up @@ -48,8 +48,6 @@ impl WebsocketsRequest {
{
let mut err = None;
let mut head = RequestHead::default();
head.method = Method::GET;
head.version = Version::HTTP_11;

match Uri::try_from(uri) {
Ok(uri) => head.uri = uri,
Expand Down
62 changes: 28 additions & 34 deletions ntex/src/http/h1/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ where
}

// process incoming bytes stream
let mut not_completed = !this.inner.poll_read(cx)?;
this.inner.decode_payload()?;
let mut not_completed = !this.inner.poll_read(cx);
this.inner.decode_payload();

loop {
// process incoming bytes stream, but only if
// previous iteration didnt read whole buffer
if not_completed {
not_completed = !this.inner.poll_read(cx)?;
not_completed = !this.inner.poll_read(cx);
}

let st = match this.call.project() {
Expand All @@ -286,10 +286,10 @@ where
// to read more data (ie serevice future can wait for payload data)
if this.inner.req_payload.is_some() && not_completed {
// read more from io stream
not_completed = !this.inner.poll_read(cx)?;
not_completed = !this.inner.poll_read(cx);

// more payload chunks has been decoded
if this.inner.decode_payload()? {
if this.inner.decode_payload() {
// restore consumed future
this = self.as_mut().project();
fut = {
Expand Down Expand Up @@ -353,7 +353,7 @@ where
let write = if !this.inner.flags.contains(Flags::STARTED) {
PollWrite::AllowNext
} else {
this.inner.decode_payload()?;
this.inner.decode_payload();
this.inner.poll_write(cx)?
};
match write {
Expand Down Expand Up @@ -421,9 +421,7 @@ where
}

// keep-alive book-keeping
if this.inner.ka_timer.is_some()
&& this.inner.poll_keepalive(cx, idle)?
{
if this.inner.ka_timer.is_some() && this.inner.poll_keepalive(cx, idle) {
this.inner.poll_shutdown(cx)
} else {
Poll::Pending
Expand Down Expand Up @@ -648,7 +646,7 @@ where
}

/// Read data from io stream
fn poll_read(&mut self, cx: &mut Context<'_>) -> Result<bool, DispatchError> {
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool {
let mut completed = false;

// read socket data into a buf
Expand All @@ -663,7 +661,7 @@ where
.map(|info| info.need_read(cx) == PayloadStatus::Read)
.unwrap_or(true)
{
return Ok(false);
return false;
}

// read data from socket
Expand Down Expand Up @@ -703,7 +701,7 @@ where
}
}

Ok(completed)
completed
}

fn internal_error(&mut self, msg: &'static str) -> DispatcherMessage {
Expand All @@ -726,12 +724,12 @@ where
DispatcherMessage::Error(Response::BadRequest().finish().drop_body())
}

fn decode_payload(&mut self) -> Result<bool, DispatchError> {
fn decode_payload(&mut self) -> bool {
if self.flags.contains(Flags::READ_EOF)
|| self.req_payload.is_none()
|| self.read_buf.is_empty()
{
return Ok(false);
return false;
}

let mut updated = false;
Expand Down Expand Up @@ -772,12 +770,12 @@ where
}
}

Ok(updated)
updated
}

fn decode_message(&mut self) -> Result<Option<DispatcherMessage>, DispatchError> {
fn decode_message(&mut self) -> Option<DispatcherMessage> {
if self.flags.contains(Flags::READ_EOF) || self.read_buf.is_empty() {
return Ok(None);
return None;
}

match self.codec.decode(&mut self.read_buf) {
Expand All @@ -797,7 +795,7 @@ where
// handle upgrade request
if pl == MessageType::Stream && self.config.upgrade.is_some() {
self.flags.insert(Flags::STOP_READING);
Ok(Some(DispatcherMessage::Upgrade(req)))
Some(DispatcherMessage::Upgrade(req))
} else {
// handle request with payload
if pl == MessageType::Payload || pl == MessageType::Stream {
Expand All @@ -808,32 +806,28 @@ where
self.req_payload = Some(ps);
}

Ok(Some(DispatcherMessage::Request(req)))
Some(DispatcherMessage::Request(req))
}
}
Message::Chunk(_) => Ok(Some(self.internal_error(
Message::Chunk(_) => Some(self.internal_error(
"Internal server error: unexpected payload chunk",
))),
)),
}
}
Ok(None) => {
self.flags.insert(Flags::READ_EOF);
Ok(None)
None
}
Err(e) => Ok(Some(self.decode_error(e))),
Err(e) => Some(self.decode_error(e)),
}
}

/// keep-alive timer
fn poll_keepalive(
&mut self,
cx: &mut Context<'_>,
idle: bool,
) -> Result<bool, DispatchError> {
fn poll_keepalive(&mut self, cx: &mut Context<'_>, idle: bool) -> bool {
let ka_timer = self.ka_timer.as_mut().unwrap();
// do nothing for disconnected or upgrade socket or if keep-alive timer is disabled
if self.flags.contains(Flags::DISCONNECT) {
return Ok(false);
return false;
}
// slow request timeout
else if !self.flags.contains(Flags::STARTED) {
Expand All @@ -845,7 +839,7 @@ where
ResponseBody::Other(Body::Empty),
);
self.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
return Ok(true);
return true;
}
}
// normal keep-alive, but only if we are not processing any requests
Expand All @@ -857,7 +851,7 @@ where
if self.write_buf.is_empty() {
trace!("Keep-alive timeout, close connection");
self.flags.insert(Flags::SHUTDOWN);
return Ok(true);
return true;
} else if let Some(dl) = self.config.keep_alive_expire() {
// extend keep-alive timer
ka_timer.reset(dl);
Expand All @@ -868,7 +862,7 @@ where
let _ = Pin::new(ka_timer).poll(cx);
}
}
Ok(false)
false
}

fn process_response(
Expand All @@ -888,11 +882,11 @@ where
&mut self,
io: CallProcess<S, X, U>,
) -> Result<CallProcess<S, X, U>, DispatchError> {
while let Some(msg) = self.decode_message()? {
while let Some(msg) = self.decode_message() {
return match msg {
DispatcherMessage::Request(req) => {
if self.req_payload.is_some() {
self.decode_payload()?;
self.decode_payload();
}

// Handle `EXPECT: 100-Continue` header
Expand Down
Loading

0 comments on commit 1e2bc4a

Please sign in to comment.