From a5bbc455c05ca3b7b978937367e32b9ffef49b95 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 18 Jun 2018 04:41:41 +0600 Subject: [PATCH] cleanup mut transform --- src/fs.rs | 6 +++--- src/httprequest.rs | 24 ++++++++++-------------- src/pipeline.rs | 2 +- src/server/h1.rs | 2 +- src/server/h1decoder.rs | 2 +- src/server/h2.rs | 2 +- src/server/helpers.rs | 9 +++------ 7 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index 68d6977c..9962f68b 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1251,16 +1251,16 @@ mod tests { #[test] fn test_redirect_to_index_nested() { - let mut st = StaticFiles::new(".").index_file("Cargo.toml"); + let mut st = StaticFiles::new(".").index_file("mod.rs"); let mut req = HttpRequest::default(); - req.match_info_mut().add("tail", "tools/wsload"); + req.match_info_mut().add("tail", "src/client"); let resp = st.handle(req).respond_to(&HttpRequest::default()).unwrap(); let resp = resp.as_msg(); assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!( resp.headers().get(header::LOCATION).unwrap(), - "/tools/wsload/Cargo.toml" + "/src/client/mod.rs" ); } diff --git a/src/httprequest.rs b/src/httprequest.rs index 519e085f..cf6869f2 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -170,23 +170,16 @@ impl HttpRequest { /// get mutable reference for inner message /// mutable reference should not be returned as result for request's method - #[inline(always)] - #[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))] - pub(crate) fn as_mut(&self) -> &mut HttpInnerMessage { + #[inline] + pub(crate) fn as_mut(&mut self) -> &mut HttpInnerMessage { self.0.get_mut() } - #[inline(always)] - #[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))] + #[inline] fn as_ref(&self) -> &HttpInnerMessage { self.0.get_ref() } - #[inline] - pub(crate) fn get_inner(&mut self) -> &mut HttpInnerMessage { - self.as_mut() - } - /// Shared application state #[inline] pub fn state(&self) -> &S { @@ -278,7 +271,8 @@ impl HttpRequest { /// Get *ConnectionInfo* for correct request. pub fn connection_info(&self) -> &ConnectionInfo { if self.extensions().get::().is_none() { - self.as_mut() + let mut req = self.clone(); + req.as_mut() .extensions .insert(Info(ConnectionInfo::new(self))); } @@ -384,7 +378,8 @@ impl HttpRequest { for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) { query.insert(key.as_ref().to_string(), val.to_string()); } - self.as_mut().extensions.insert(Query(query)); + let mut req = self.clone(); + req.as_mut().extensions.insert(Query(query)); } &self.extensions().get::().unwrap().0 } @@ -404,7 +399,8 @@ impl HttpRequest { /// Load request cookies. pub fn cookies(&self) -> Result<&Vec>, CookieParseError> { if self.extensions().get::().is_none() { - let msg = self.as_mut(); + let mut req = self.clone(); + let msg = req.as_mut(); let mut cookies = Vec::new(); for hdr in msg.headers.get_all(header::COOKIE) { let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?; @@ -479,7 +475,7 @@ impl HttpRequest { } #[cfg(test)] - pub(crate) fn payload(&self) -> &Payload { + pub(crate) fn payload(&mut self) -> &Payload { let msg = self.as_mut(); if msg.payload.is_none() { msg.payload = Some(Payload::empty()); diff --git a/src/pipeline.rs b/src/pipeline.rs index 8be7ee83..e5eb51d5 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -487,7 +487,7 @@ impl ProcessResponse { self.resp.content_encoding().unwrap_or(info.encoding); let result = match io.start( - info.req_mut().get_inner(), + info.req_mut().as_mut(), &mut self.resp, encoding, ) { diff --git a/src/server/h1.rs b/src/server/h1.rs index e5fa2fe9..689d64dc 100644 --- a/src/server/h1.rs +++ b/src/server/h1.rs @@ -306,7 +306,7 @@ where pub fn parse(&mut self) { 'outer: loop { match self.decoder.decode(&mut self.buf, &self.settings) { - Ok(Some(Message::Message { msg, payload })) => { + Ok(Some(Message::Message { mut msg, payload })) => { self.flags.insert(Flags::STARTED); if payload { diff --git a/src/server/h1decoder.rs b/src/server/h1decoder.rs index 976a079e..77da36af 100644 --- a/src/server/h1decoder.rs +++ b/src/server/h1decoder.rs @@ -120,7 +120,7 @@ impl H1Decoder { let slice = buf.split_to(len).freeze(); // convert headers - let msg = settings.get_http_message(); + let mut msg = settings.get_http_message(); { let msg_mut = msg.get_mut(); msg_mut diff --git a/src/server/h2.rs b/src/server/h2.rs index a73cc599..e194bc7d 100644 --- a/src/server/h2.rs +++ b/src/server/h2.rs @@ -307,7 +307,7 @@ impl Entry { // Payload and Content-Encoding let (psender, payload) = Payload::new(false); - let msg = settings.get_http_message(); + let mut msg = settings.get_http_message(); msg.get_mut().url = Url::new(parts.uri); msg.get_mut().method = parts.method; msg.get_mut().version = parts.version; diff --git a/src/server/helpers.rs b/src/server/helpers.rs index f447a6ca..cf497edd 100644 --- a/src/server/helpers.rs +++ b/src/server/helpers.rs @@ -74,16 +74,13 @@ impl SharedHttpInnerMessage { SharedHttpInnerMessage(Some(msg), Some(pool)) } - #[inline(always)] - #[allow(mutable_transmutes)] - #[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))] - pub fn get_mut(&self) -> &mut HttpInnerMessage { + #[inline] + pub fn get_mut(&mut self) -> &mut HttpInnerMessage { let r: &HttpInnerMessage = self.0.as_ref().unwrap().as_ref(); unsafe { &mut *(r as *const _ as *mut _) } } - #[inline(always)] - #[cfg_attr(feature = "cargo-clippy", allow(inline_always))] + #[inline] pub fn get_ref(&self) -> &HttpInnerMessage { self.0.as_ref().unwrap() }