From 3987fbe50838ed161e2153d2d10e37888bb55616 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Date: Sun, 27 Dec 2020 06:09:56 +0100 Subject: [PATCH] Fix minor issues (#33) * Fix unused Default trait logic * Remove `put_slice` in favor of `extend_from_slice` * Fix spelling --- ntex-router/src/tree.rs | 2 +- ntex/src/http/body.rs | 18 +++++++----------- ntex/src/http/client/response.rs | 2 +- ntex/src/http/encoding/encoder.rs | 2 +- ntex/src/http/h1/dispatcher.rs | 2 +- ntex/src/http/h1/encoder.rs | 26 +++++++++++++------------- ntex/src/http/payload.rs | 8 +++++++- ntex/src/http/request.rs | 2 +- ntex/src/server/builder.rs | 4 ++-- ntex/src/web/app_service.rs | 4 ++-- ntex/src/web/resource.rs | 2 +- ntex/src/web/route.rs | 2 +- ntex/src/web/scope.rs | 2 +- ntex/src/web/service.rs | 2 +- ntex/src/ws/frame.rs | 10 +++++----- 15 files changed, 45 insertions(+), 43 deletions(-) diff --git a/ntex-router/src/tree.rs b/ntex-router/src/tree.rs index bfc18a0b..610f97f7 100644 --- a/ntex-router/src/tree.rs +++ b/ntex-router/src/tree.rs @@ -123,7 +123,7 @@ impl Tree { if p < self.key.len() { let child = Tree { key: self.key.split_off(p), - value: mem::replace(&mut self.value, Vec::new()), + value: mem::take(&mut self.value), children: mem::take(&mut self.children), }; self.children.push(child); diff --git a/ntex/src/http/body.rs b/ntex/src/http/body.rs index af70b6b1..27698b4a 100644 --- a/ntex/src/http/body.rs +++ b/ntex/src/http/body.rs @@ -177,7 +177,7 @@ impl MessageBody for Body { if len == 0 { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(mem::replace(bin, Bytes::new())))) + Poll::Ready(Some(Ok(mem::take(bin)))) } } Body::Message(ref mut body) => body.poll_next_chunk(cx), @@ -286,7 +286,7 @@ impl MessageBody for Bytes { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(mem::replace(self, Bytes::new())))) + Poll::Ready(Some(Ok(mem::take(self)))) } } } @@ -303,7 +303,7 @@ impl MessageBody for BytesMut { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(mem::replace(self, BytesMut::new()).freeze()))) + Poll::Ready(Some(Ok(mem::take(self).freeze()))) } } } @@ -320,9 +320,7 @@ impl MessageBody for &'static str { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(Bytes::from_static( - mem::replace(self, "").as_ref(), - )))) + Poll::Ready(Some(Ok(Bytes::from_static(mem::take(self).as_ref())))) } } } @@ -339,7 +337,7 @@ impl MessageBody for &'static [u8] { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(Bytes::from_static(mem::replace(self, b""))))) + Poll::Ready(Some(Ok(Bytes::from_static(mem::take(self))))) } } } @@ -356,7 +354,7 @@ impl MessageBody for Vec { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(Bytes::from(mem::replace(self, Vec::new()))))) + Poll::Ready(Some(Ok(Bytes::from(mem::take(self))))) } } } @@ -373,9 +371,7 @@ impl MessageBody for String { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(Bytes::from( - mem::replace(self, String::new()).into_bytes(), - )))) + Poll::Ready(Some(Ok(Bytes::from(mem::take(self).into_bytes())))) } } } diff --git a/ntex/src/http/client/response.rs b/ntex/src/http/client/response.rs index b5415908..f80b9e1e 100644 --- a/ntex/src/http/client/response.rs +++ b/ntex/src/http/client/response.rs @@ -103,7 +103,7 @@ impl ClientResponse { /// Get response's payload pub fn take_payload(&mut self) -> Payload { - std::mem::replace(&mut self.payload, Payload::None) + std::mem::take(&mut self.payload) } /// Request extensions diff --git a/ntex/src/http/encoding/encoder.rs b/ntex/src/http/encoding/encoder.rs index 80e2f040..70411367 100644 --- a/ntex/src/http/encoding/encoder.rs +++ b/ntex/src/http/encoding/encoder.rs @@ -120,7 +120,7 @@ impl MessageBody for Encoder { if b.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(std::mem::replace(b, Bytes::new())))) + Poll::Ready(Some(Ok(std::mem::take(b)))) } } EncoderBody::Stream(ref mut b) => b.poll_next_chunk(cx), diff --git a/ntex/src/http/h1/dispatcher.rs b/ntex/src/http/h1/dispatcher.rs index 3d528873..141f82d3 100644 --- a/ntex/src/http/h1/dispatcher.rs +++ b/ntex/src/http/h1/dispatcher.rs @@ -283,7 +283,7 @@ where }, Poll::Pending => { // if read back-pressure is enabled, we might need - // to read more data (ie serevice future can wait for payload data) + // to read more data (ie service 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); diff --git a/ntex/src/http/h1/encoder.rs b/ntex/src/http/h1/encoder.rs index 81625f2a..ea58ac7f 100644 --- a/ntex/src/http/h1/encoder.rs +++ b/ntex/src/http/h1/encoder.rs @@ -68,27 +68,27 @@ pub(super) trait MessageType: Sized { } } match length { - BodySize::None => dst.put_slice(b"\r\n"), - BodySize::Empty => dst.put_slice(b"\r\ncontent-length: 0\r\n"), + BodySize::None => dst.extend_from_slice(b"\r\n"), + BodySize::Empty => dst.extend_from_slice(b"\r\ncontent-length: 0\r\n"), BodySize::Sized(len) => write_content_length(len, dst), BodySize::Stream => { if chunked { - dst.put_slice(b"\r\ntransfer-encoding: chunked\r\n") + dst.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n") } else { skip_len = false; - dst.put_slice(b"\r\n"); + dst.extend_from_slice(b"\r\n"); } } } // Connection match ctype { - ConnectionType::Upgrade => dst.put_slice(b"connection: upgrade\r\n"), + ConnectionType::Upgrade => dst.extend_from_slice(b"connection: upgrade\r\n"), ConnectionType::KeepAlive if version < Version::HTTP_11 => { - dst.put_slice(b"connection: keep-alive\r\n") + dst.extend_from_slice(b"connection: keep-alive\r\n") } ConnectionType::Close if version >= Version::HTTP_11 => { - dst.put_slice(b"connection: close\r\n") + dst.extend_from_slice(b"connection: close\r\n") } _ => (), } @@ -215,7 +215,7 @@ impl MessageType for Response<()> { // status line write_status_line(head.version, head.status.as_u16(), dst); - dst.put_slice(reason); + dst.extend_from_slice(reason); Ok(()) } } @@ -471,7 +471,7 @@ fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesMut) { } } - bytes.put_slice(&buf); + bytes.extend_from_slice(&buf); if four { bytes.put_u8(b' '); } @@ -485,7 +485,7 @@ fn write_content_length(mut n: u64, bytes: &mut BytesMut) { b'n', b'g', b't', b'h', b':', b' ', b'0', b'\r', b'\n', ]; buf[18] = (n as u8) + b'0'; - bytes.put_slice(&buf); + bytes.extend_from_slice(&buf); } else if n < 100 { let mut buf: [u8; 22] = [ b'\r', b'\n', b'c', b'o', b'n', b't', b'e', b'n', b't', b'-', b'l', b'e', @@ -499,7 +499,7 @@ fn write_content_length(mut n: u64, bytes: &mut BytesMut) { 2, ); } - bytes.put_slice(&buf); + bytes.extend_from_slice(&buf); } else if n < 1000 { let mut buf: [u8; 23] = [ b'\r', b'\n', b'c', b'o', b'n', b't', b'e', b'n', b't', b'-', b'l', b'e', @@ -519,9 +519,9 @@ fn write_content_length(mut n: u64, bytes: &mut BytesMut) { // decode last 1 buf[18] = (n as u8) + b'0'; - bytes.put_slice(&buf); + bytes.extend_from_slice(&buf); } else { - bytes.put_slice(b"\r\ncontent-length: "); + bytes.extend_from_slice(b"\r\ncontent-length: "); unsafe { convert_usize(n, bytes) }; } } diff --git a/ntex/src/http/payload.rs b/ntex/src/http/payload.rs index f016cfc2..c079d04e 100644 --- a/ntex/src/http/payload.rs +++ b/ntex/src/http/payload.rs @@ -19,6 +19,12 @@ pub enum Payload { Stream(S), } +impl Default for Payload { + fn default() -> Self { + Payload::None + } +} + impl From for Payload { fn from(v: crate::http::h1::Payload) -> Self { Payload::H1(v) @@ -57,7 +63,7 @@ impl fmt::Debug for Payload { impl Payload { /// Takes current payload and replaces it with `None` value pub fn take(&mut self) -> Payload { - std::mem::replace(self, Payload::None) + std::mem::take(self) } } diff --git a/ntex/src/http/request.rs b/ntex/src/http/request.rs index 55de97f3..99412eab 100644 --- a/ntex/src/http/request.rs +++ b/ntex/src/http/request.rs @@ -143,7 +143,7 @@ impl

Request

{ /// Get request's payload pub fn take_payload(&mut self) -> Payload

{ - std::mem::replace(&mut self.payload, Payload::None) + std::mem::take(&mut self.payload) } /// Create new Request instance diff --git a/ntex/src/server/builder.rs b/ntex/src/server/builder.rs index 879cb7ed..e43134ce 100644 --- a/ntex/src/server/builder.rs +++ b/ntex/src/server/builder.rs @@ -279,7 +279,7 @@ impl ServerBuilder { info!("Starting \"{}\" service on {}", sock.1, sock.2); } self.accept.start( - mem::replace(&mut self.sockets, Vec::new()) + mem::take(&mut self.sockets) .into_iter() .map(|t| (t.0, t.2)) .collect(), @@ -358,7 +358,7 @@ impl ServerBuilder { // stop accept thread self.accept.send(Command::Stop); - let notify = std::mem::replace(&mut self.notify, Vec::new()); + let notify = std::mem::take(&mut self.notify); // stop workers if !self.workers.is_empty() && graceful { diff --git a/ntex/src/web/app_service.rs b/ntex/src/web/app_service.rs index 204948fc..abe15ad3 100644 --- a/ntex/src/web/app_service.rs +++ b/ntex/src/web/app_service.rs @@ -88,7 +88,7 @@ where WebServiceConfig::new(config, default.clone(), self.data.clone()); // register services - std::mem::replace(&mut *self.services.borrow_mut(), Vec::new()) + std::mem::take(&mut *self.services.borrow_mut()) .into_iter() .for_each(|mut srv| srv.register(&mut config)); @@ -112,7 +112,7 @@ where }); // external resources - for mut rdef in std::mem::replace(&mut *self.external.borrow_mut(), Vec::new()) { + for mut rdef in std::mem::take(&mut *self.external.borrow_mut()) { rmap.add(&mut rdef, None); } diff --git a/ntex/src/web/resource.rs b/ntex/src/web/resource.rs index 6d8be6fe..2d6954f1 100644 --- a/ntex/src/web/resource.rs +++ b/ntex/src/web/resource.rs @@ -392,7 +392,7 @@ where let guards = if self.guards.is_empty() { None } else { - Some(std::mem::replace(&mut self.guards, Vec::new())) + Some(std::mem::take(&mut self.guards)) }; let mut rdef = if config.is_root() || !self.rdef.is_empty() { ResourceDef::new(insert_slesh(self.rdef.clone())) diff --git a/ntex/src/web/route.rs b/ntex/src/web/route.rs index 653b5473..75c11126 100644 --- a/ntex/src/web/route.rs +++ b/ntex/src/web/route.rs @@ -44,7 +44,7 @@ impl Route { .push(Box::new(guard::Method(m.clone()))); } - mem::replace(Rc::get_mut(&mut self.guards).unwrap(), Vec::new()) + mem::take(Rc::get_mut(&mut self.guards).unwrap()) } pub(super) fn service(&self) -> RouteService { diff --git a/ntex/src/web/scope.rs b/ntex/src/web/scope.rs index 79594a25..a9b1eed4 100644 --- a/ntex/src/web/scope.rs +++ b/ntex/src/web/scope.rs @@ -437,7 +437,7 @@ where let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef)); // external resources - for mut rdef in std::mem::replace(&mut self.external, Vec::new()) { + for mut rdef in std::mem::take(&mut self.external) { rmap.add(&mut rdef, None); } diff --git a/ntex/src/web/service.rs b/ntex/src/web/service.rs index 7dada9bc..eabaa0d2 100644 --- a/ntex/src/web/service.rs +++ b/ntex/src/web/service.rs @@ -258,7 +258,7 @@ where let guards = if self.guards.is_empty() { None } else { - Some(std::mem::replace(&mut self.guards, Vec::new())) + Some(std::mem::take(&mut self.guards)) }; let mut rdef = if config.is_root() || !self.rdef.is_empty() { diff --git a/ntex/src/ws/frame.rs b/ntex/src/ws/frame.rs index 7c289890..c0281f25 100644 --- a/ntex/src/ws/frame.rs +++ b/ntex/src/ws/frame.rs @@ -175,25 +175,25 @@ impl Parser { if payload_len < 126 { dst.reserve(p_len + 2 + if mask { 4 } else { 0 }); - dst.put_slice(&[one, two | payload_len as u8]); + dst.extend_from_slice(&[one, two | payload_len as u8]); } else if payload_len <= 65_535 { dst.reserve(p_len + 4 + if mask { 4 } else { 0 }); - dst.put_slice(&[one, two | 126]); + dst.extend_from_slice(&[one, two | 126]); dst.put_u16(payload_len as u16); } else { dst.reserve(p_len + 10 + if mask { 4 } else { 0 }); - dst.put_slice(&[one, two | 127]); + dst.extend_from_slice(&[one, two | 127]); dst.put_u64(payload_len as u64); }; if mask { let mask = rand::random::(); dst.put_u32_le(mask); - dst.put_slice(payload.as_ref()); + dst.extend_from_slice(payload.as_ref()); let pos = dst.len() - payload_len; apply_mask(&mut dst[pos..], mask); } else { - dst.put_slice(payload.as_ref()); + dst.extend_from_slice(payload.as_ref()); } }