1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-01-17 20:56:08 +00:00

use mem::take where possible (#1507)

This commit is contained in:
Rob Ede 2020-05-17 02:54:42 +01:00 committed by GitHub
parent 201090d7a2
commit f3b0233477
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 20 additions and 32 deletions

View file

@ -189,7 +189,7 @@ impl MessageBody for Body {
if len == 0 { if len == 0 {
Poll::Ready(None) Poll::Ready(None)
} else { } 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), 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() { if self.is_empty() {
Poll::Ready(None) Poll::Ready(None)
} else { } 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() { if self.is_empty() {
Poll::Ready(None) Poll::Ready(None)
} else { } else {
Poll::Ready(Some(Ok( Poll::Ready(Some(Ok(mem::take(self.get_mut()).freeze())))
mem::replace(self.get_mut(), BytesMut::new()).freeze()
)))
} }
} }
} }
@ -344,7 +342,7 @@ impl MessageBody for &'static str {
Poll::Ready(None) Poll::Ready(None)
} else { } else {
Poll::Ready(Some(Ok(Bytes::from_static( 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<u8> {
if self.is_empty() { if self.is_empty() {
Poll::Ready(None) Poll::Ready(None)
} else { } else {
Poll::Ready(Some(Ok(Bytes::from(mem::replace( Poll::Ready(Some(Ok(Bytes::from(mem::take(self.get_mut())))))
self.get_mut(),
Vec::new(),
)))))
} }
} }
} }
@ -383,7 +378,7 @@ impl MessageBody for String {
Poll::Ready(None) Poll::Ready(None)
} else { } else {
Poll::Ready(Some(Ok(Bytes::from( Poll::Ready(Some(Ok(Bytes::from(
mem::replace(self.get_mut(), String::new()).into_bytes(), mem::take(self.get_mut()).into_bytes(),
)))) ))))
} }
} }

View file

@ -1,5 +1,5 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::mem::replace; use std::mem;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime};
@ -273,7 +273,7 @@ impl CookieJar {
)] )]
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.delta_cookies.clear(); 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); self.remove(delta.cookie);
} }
} }

View file

@ -106,7 +106,7 @@ impl<B: MessageBody> MessageBody for EncoderBody<B> {
if b.is_empty() { if b.is_empty() {
Poll::Ready(None) Poll::Ready(None)
} else { } 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), EncoderBody::Stream(b) => b.poll_next(cx),

View file

@ -1,6 +1,3 @@
// Because MSRV is 1.39.0.
#![allow(clippy::mem_replace_with_default)]
use std::collections::VecDeque; use std::collections::VecDeque;
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
@ -795,13 +792,10 @@ where
let inner_p = inner.as_mut().project(); let inner_p = inner.as_mut().project();
let mut parts = FramedParts::with_read_buf( let mut parts = FramedParts::with_read_buf(
inner_p.io.take().unwrap(), inner_p.io.take().unwrap(),
std::mem::replace(inner_p.codec, Codec::default()), std::mem::take(inner_p.codec),
std::mem::replace(inner_p.read_buf, BytesMut::default()), std::mem::take(inner_p.read_buf),
);
parts.write_buf = std::mem::replace(
inner_p.write_buf,
BytesMut::default(),
); );
parts.write_buf = std::mem::take(inner_p.write_buf);
let framed = Framed::from_parts(parts); let framed = Framed::from_parts(parts);
let upgrade = let upgrade =
inner_p.upgrade.take().unwrap().call((req, framed)); inner_p.upgrade.take().unwrap().call((req, framed));

View file

@ -200,8 +200,7 @@ impl Session {
.extensions() .extensions()
.get::<Rc<RefCell<SessionInner>>>() .get::<Rc<RefCell<SessionInner>>>()
{ {
let state = let state = std::mem::take(&mut s_impl.borrow_mut().state);
std::mem::replace(&mut s_impl.borrow_mut().state, HashMap::new());
(s_impl.borrow().status.clone(), Some(state.into_iter())) (s_impl.borrow().status.clone(), Some(state.into_iter()))
} else { } else {
(SessionStatus::Unchanged, None) (SessionStatus::Unchanged, None)

View file

@ -12,7 +12,7 @@ use actix_service::{fn_service, Service, ServiceFactory};
use futures::future::{join_all, ok, FutureExt, LocalBoxFuture}; use futures::future::{join_all, ok, FutureExt, LocalBoxFuture};
use crate::config::{AppConfig, AppService}; use crate::config::{AppConfig, AppService};
use crate::data::{FnDataFactory, DataFactory}; use crate::data::{DataFactory, FnDataFactory};
use crate::error::Error; use crate::error::Error;
use crate::guard::Guard; use crate::guard::Guard;
use crate::request::{HttpRequest, HttpRequestPool}; use crate::request::{HttpRequest, HttpRequestPool};
@ -76,7 +76,7 @@ where
let mut config = AppService::new(config, default.clone(), self.data.clone()); let mut config = AppService::new(config, default.clone(), self.data.clone());
// register services // register services
std::mem::replace(&mut *self.services.borrow_mut(), Vec::new()) std::mem::take(&mut *self.services.borrow_mut())
.into_iter() .into_iter()
.for_each(|mut srv| srv.register(&mut config)); .for_each(|mut srv| srv.register(&mut config));
@ -99,7 +99,7 @@ where
}); });
// external resources // 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); rmap.add(&mut rdef, None);
} }

View file

@ -379,7 +379,7 @@ where
let guards = if self.guards.is_empty() { let guards = if self.guards.is_empty() {
None None
} else { } 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() { let mut rdef = if config.is_root() || !self.rdef.is_empty() {
ResourceDef::new(insert_slash(self.rdef.clone())) ResourceDef::new(insert_slash(self.rdef.clone()))

View file

@ -56,7 +56,7 @@ impl Route {
} }
pub(crate) fn take_guards(&mut self) -> Vec<Box<dyn Guard>> { pub(crate) fn take_guards(&mut self) -> Vec<Box<dyn Guard>> {
std::mem::replace(Rc::get_mut(&mut self.guards).unwrap(), Vec::new()) std::mem::take(Rc::get_mut(&mut self.guards).unwrap())
} }
} }

View file

@ -429,7 +429,7 @@ where
let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef)); let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef));
// external resources // 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); rmap.add(&mut rdef, None);
} }

View file

@ -515,7 +515,7 @@ where
let guards = if self.guards.is_empty() { let guards = if self.guards.is_empty() {
None None
} else { } 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() { let mut rdef = if config.is_root() || !self.rdef.is_empty() {