From f3b023347776b9ea727b632135d777313efdaea6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 17 May 2020 02:54:42 +0100 Subject: [PATCH] use mem::take where possible (#1507) --- actix-http/src/body.rs | 17 ++++++----------- actix-http/src/cookie/jar.rs | 4 ++-- actix-http/src/encoding/encoder.rs | 2 +- actix-http/src/h1/dispatcher.rs | 12 +++--------- actix-session/src/lib.rs | 3 +-- src/app_service.rs | 6 +++--- src/resource.rs | 2 +- src/route.rs | 2 +- src/scope.rs | 2 +- src/service.rs | 2 +- 10 files changed, 20 insertions(+), 32 deletions(-) diff --git a/actix-http/src/body.rs b/actix-http/src/body.rs index c581db604..f887b53bb 100644 --- a/actix-http/src/body.rs +++ b/actix-http/src/body.rs @@ -189,7 +189,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) => Pin::new(body.as_mut()).poll_next(cx), @@ -307,7 +307,7 @@ impl MessageBody for Bytes { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(mem::replace(self.get_mut(), Bytes::new())))) + Poll::Ready(Some(Ok(mem::take(self.get_mut())))) } } } @@ -324,9 +324,7 @@ impl MessageBody for BytesMut { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok( - mem::replace(self.get_mut(), BytesMut::new()).freeze() - ))) + Poll::Ready(Some(Ok(mem::take(self.get_mut()).freeze()))) } } } @@ -344,7 +342,7 @@ impl MessageBody for &'static str { Poll::Ready(None) } else { Poll::Ready(Some(Ok(Bytes::from_static( - mem::replace(self.get_mut(), "").as_ref(), + mem::take(self.get_mut()).as_ref(), )))) } } @@ -362,10 +360,7 @@ impl MessageBody for Vec { if self.is_empty() { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(Bytes::from(mem::replace( - self.get_mut(), - Vec::new(), - ))))) + Poll::Ready(Some(Ok(Bytes::from(mem::take(self.get_mut()))))) } } } @@ -383,7 +378,7 @@ impl MessageBody for String { Poll::Ready(None) } else { Poll::Ready(Some(Ok(Bytes::from( - mem::replace(self.get_mut(), String::new()).into_bytes(), + mem::take(self.get_mut()).into_bytes(), )))) } } diff --git a/actix-http/src/cookie/jar.rs b/actix-http/src/cookie/jar.rs index 0c76c1cfe..fbefa1bbf 100644 --- a/actix-http/src/cookie/jar.rs +++ b/actix-http/src/cookie/jar.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std::mem::replace; +use std::mem; use time::{Duration, OffsetDateTime}; @@ -273,7 +273,7 @@ impl CookieJar { )] pub fn clear(&mut self) { self.delta_cookies.clear(); - for delta in replace(&mut self.original_cookies, HashSet::new()) { + for delta in mem::take(&mut self.original_cookies) { self.remove(delta.cookie); } } diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 72bb7d603..ef69aa039 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -106,7 +106,7 @@ impl MessageBody for EncoderBody { 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(b) => b.poll_next(cx), diff --git a/actix-http/src/h1/dispatcher.rs b/actix-http/src/h1/dispatcher.rs index 88f11c7c7..c95000bf9 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -1,6 +1,3 @@ -// Because MSRV is 1.39.0. -#![allow(clippy::mem_replace_with_default)] - use std::collections::VecDeque; use std::future::Future; use std::pin::Pin; @@ -795,13 +792,10 @@ where let inner_p = inner.as_mut().project(); let mut parts = FramedParts::with_read_buf( inner_p.io.take().unwrap(), - std::mem::replace(inner_p.codec, Codec::default()), - std::mem::replace(inner_p.read_buf, BytesMut::default()), - ); - parts.write_buf = std::mem::replace( - inner_p.write_buf, - BytesMut::default(), + std::mem::take(inner_p.codec), + std::mem::take(inner_p.read_buf), ); + parts.write_buf = std::mem::take(inner_p.write_buf); let framed = Framed::from_parts(parts); let upgrade = inner_p.upgrade.take().unwrap().call((req, framed)); diff --git a/actix-session/src/lib.rs b/actix-session/src/lib.rs index b6e5dd331..e2bf0143b 100644 --- a/actix-session/src/lib.rs +++ b/actix-session/src/lib.rs @@ -200,8 +200,7 @@ impl Session { .extensions() .get::>>() { - let state = - std::mem::replace(&mut s_impl.borrow_mut().state, HashMap::new()); + let state = std::mem::take(&mut s_impl.borrow_mut().state); (s_impl.borrow().status.clone(), Some(state.into_iter())) } else { (SessionStatus::Unchanged, None) diff --git a/src/app_service.rs b/src/app_service.rs index 2d64bed3e..693624ba0 100644 --- a/src/app_service.rs +++ b/src/app_service.rs @@ -12,7 +12,7 @@ use actix_service::{fn_service, Service, ServiceFactory}; use futures::future::{join_all, ok, FutureExt, LocalBoxFuture}; use crate::config::{AppConfig, AppService}; -use crate::data::{FnDataFactory, DataFactory}; +use crate::data::{DataFactory, FnDataFactory}; use crate::error::Error; use crate::guard::Guard; use crate::request::{HttpRequest, HttpRequestPool}; @@ -76,7 +76,7 @@ where let mut config = AppService::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)); @@ -99,7 +99,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/src/resource.rs b/src/resource.rs index 477f0bfba..4c0e26c18 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -379,7 +379,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_slash(self.rdef.clone())) diff --git a/src/route.rs b/src/route.rs index f7e391746..11455630c 100644 --- a/src/route.rs +++ b/src/route.rs @@ -56,7 +56,7 @@ impl Route { } pub(crate) fn take_guards(&mut self) -> Vec> { - std::mem::replace(Rc::get_mut(&mut self.guards).unwrap(), Vec::new()) + std::mem::take(Rc::get_mut(&mut self.guards).unwrap()) } } diff --git a/src/scope.rs b/src/scope.rs index 407d4946d..5afca61da 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -429,7 +429,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/src/service.rs b/src/service.rs index 8dc9fa93d..c0148a9b2 100644 --- a/src/service.rs +++ b/src/service.rs @@ -515,7 +515,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() {