Fix minor issues (#33)

* Fix unused Default trait logic

* Remove `put_slice` in favor of `extend_from_slice`

* Fix spelling
This commit is contained in:
Juan Aguilar 2020-12-27 06:09:56 +01:00 committed by GitHub
parent 72cc69ab95
commit 3987fbe508
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 45 additions and 43 deletions

View file

@ -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);

View file

@ -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<u8> {
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()))))
}
}
}

View file

@ -103,7 +103,7 @@ impl<S> ClientResponse<S> {
/// Get response's payload
pub fn take_payload(&mut self) -> Payload<S> {
std::mem::replace(&mut self.payload, Payload::None)
std::mem::take(&mut self.payload)
}
/// Request extensions

View file

@ -120,7 +120,7 @@ impl<B: MessageBody> MessageBody for Encoder<B> {
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),

View file

@ -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);

View file

@ -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) };
}
}

View file

@ -19,6 +19,12 @@ pub enum Payload<S = PayloadStream> {
Stream(S),
}
impl<S> Default for Payload<S> {
fn default() -> Self {
Payload::None
}
}
impl<S> From<crate::http::h1::Payload> for Payload<S> {
fn from(v: crate::http::h1::Payload) -> Self {
Payload::H1(v)
@ -57,7 +63,7 @@ impl<S> fmt::Debug for Payload<S> {
impl<S> Payload<S> {
/// Takes current payload and replaces it with `None` value
pub fn take(&mut self) -> Payload<S> {
std::mem::replace(self, Payload::None)
std::mem::take(self)
}
}

View file

@ -143,7 +143,7 @@ impl<P> Request<P> {
/// Get request's payload
pub fn take_payload(&mut self) -> Payload<P> {
std::mem::replace(&mut self.payload, Payload::None)
std::mem::take(&mut self.payload)
}
/// Create new Request instance

View file

@ -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 {

View file

@ -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);
}

View file

@ -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()))

View file

@ -44,7 +44,7 @@ impl<Err: ErrorRenderer> Route<Err> {
.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<Err> {

View file

@ -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);
}

View file

@ -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() {

View file

@ -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::<u32>();
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());
}
}