1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

cleanup mut transform

This commit is contained in:
Nikolay Kim 2018-06-18 04:41:41 +06:00
parent 6ec8352612
commit a5bbc455c0
7 changed files with 20 additions and 27 deletions

View File

@ -1251,16 +1251,16 @@ mod tests {
#[test] #[test]
fn test_redirect_to_index_nested() { 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(); 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 = st.handle(req).respond_to(&HttpRequest::default()).unwrap();
let resp = resp.as_msg(); let resp = resp.as_msg();
assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!( assert_eq!(
resp.headers().get(header::LOCATION).unwrap(), resp.headers().get(header::LOCATION).unwrap(),
"/tools/wsload/Cargo.toml" "/src/client/mod.rs"
); );
} }

View File

@ -170,23 +170,16 @@ impl<S> HttpRequest<S> {
/// get mutable reference for inner message /// get mutable reference for inner message
/// mutable reference should not be returned as result for request's method /// mutable reference should not be returned as result for request's method
#[inline(always)] #[inline]
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))] pub(crate) fn as_mut(&mut self) -> &mut HttpInnerMessage {
pub(crate) fn as_mut(&self) -> &mut HttpInnerMessage {
self.0.get_mut() self.0.get_mut()
} }
#[inline(always)] #[inline]
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
fn as_ref(&self) -> &HttpInnerMessage { fn as_ref(&self) -> &HttpInnerMessage {
self.0.get_ref() self.0.get_ref()
} }
#[inline]
pub(crate) fn get_inner(&mut self) -> &mut HttpInnerMessage {
self.as_mut()
}
/// Shared application state /// Shared application state
#[inline] #[inline]
pub fn state(&self) -> &S { pub fn state(&self) -> &S {
@ -278,7 +271,8 @@ impl<S> HttpRequest<S> {
/// Get *ConnectionInfo* for correct request. /// Get *ConnectionInfo* for correct request.
pub fn connection_info(&self) -> &ConnectionInfo { pub fn connection_info(&self) -> &ConnectionInfo {
if self.extensions().get::<Info>().is_none() { if self.extensions().get::<Info>().is_none() {
self.as_mut() let mut req = self.clone();
req.as_mut()
.extensions .extensions
.insert(Info(ConnectionInfo::new(self))); .insert(Info(ConnectionInfo::new(self)));
} }
@ -384,7 +378,8 @@ impl<S> HttpRequest<S> {
for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) { for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) {
query.insert(key.as_ref().to_string(), val.to_string()); 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::<Query>().unwrap().0 &self.extensions().get::<Query>().unwrap().0
} }
@ -404,7 +399,8 @@ impl<S> HttpRequest<S> {
/// Load request cookies. /// Load request cookies.
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> { pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
if self.extensions().get::<Query>().is_none() { if self.extensions().get::<Query>().is_none() {
let msg = self.as_mut(); let mut req = self.clone();
let msg = req.as_mut();
let mut cookies = Vec::new(); let mut cookies = Vec::new();
for hdr in msg.headers.get_all(header::COOKIE) { for hdr in msg.headers.get_all(header::COOKIE) {
let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?; let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
@ -479,7 +475,7 @@ impl<S> HttpRequest<S> {
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn payload(&self) -> &Payload { pub(crate) fn payload(&mut self) -> &Payload {
let msg = self.as_mut(); let msg = self.as_mut();
if msg.payload.is_none() { if msg.payload.is_none() {
msg.payload = Some(Payload::empty()); msg.payload = Some(Payload::empty());

View File

@ -487,7 +487,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
self.resp.content_encoding().unwrap_or(info.encoding); self.resp.content_encoding().unwrap_or(info.encoding);
let result = match io.start( let result = match io.start(
info.req_mut().get_inner(), info.req_mut().as_mut(),
&mut self.resp, &mut self.resp,
encoding, encoding,
) { ) {

View File

@ -306,7 +306,7 @@ where
pub fn parse(&mut self) { pub fn parse(&mut self) {
'outer: loop { 'outer: loop {
match self.decoder.decode(&mut self.buf, &self.settings) { 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); self.flags.insert(Flags::STARTED);
if payload { if payload {

View File

@ -120,7 +120,7 @@ impl H1Decoder {
let slice = buf.split_to(len).freeze(); let slice = buf.split_to(len).freeze();
// convert headers // convert headers
let msg = settings.get_http_message(); let mut msg = settings.get_http_message();
{ {
let msg_mut = msg.get_mut(); let msg_mut = msg.get_mut();
msg_mut msg_mut

View File

@ -307,7 +307,7 @@ impl<H: 'static> Entry<H> {
// Payload and Content-Encoding // Payload and Content-Encoding
let (psender, payload) = Payload::new(false); 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().url = Url::new(parts.uri);
msg.get_mut().method = parts.method; msg.get_mut().method = parts.method;
msg.get_mut().version = parts.version; msg.get_mut().version = parts.version;

View File

@ -74,16 +74,13 @@ impl SharedHttpInnerMessage {
SharedHttpInnerMessage(Some(msg), Some(pool)) SharedHttpInnerMessage(Some(msg), Some(pool))
} }
#[inline(always)] #[inline]
#[allow(mutable_transmutes)] pub fn get_mut(&mut self) -> &mut HttpInnerMessage {
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
pub fn get_mut(&self) -> &mut HttpInnerMessage {
let r: &HttpInnerMessage = self.0.as_ref().unwrap().as_ref(); let r: &HttpInnerMessage = self.0.as_ref().unwrap().as_ref();
unsafe { &mut *(r as *const _ as *mut _) } unsafe { &mut *(r as *const _ as *mut _) }
} }
#[inline(always)] #[inline]
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
pub fn get_ref(&self) -> &HttpInnerMessage { pub fn get_ref(&self) -> &HttpInnerMessage {
self.0.as_ref().unwrap() self.0.as_ref().unwrap()
} }