From 76b644365fd6fa74e1617dc16f6a36c1eeb85a0d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 2 May 2018 06:07:30 -0700 Subject: [PATCH] use read only ref for FromRequest; remove unnecessary static --- src/error.rs | 3 +-- src/extractor.rs | 33 ++++++++++++++------------------- src/handler.rs | 20 ++++++++++++-------- src/httprequest.rs | 4 ++-- src/json.rs | 2 +- src/with.rs | 24 ++++++++++-------------- 6 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/error.rs b/src/error.rs index 37dc3d89f..963abd3b9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -805,8 +805,7 @@ mod tests { #[test] fn test_backtrace() { - let orig = ErrorBadRequest("err"); - let e: Error = orig.into(); + let e = ErrorBadRequest("err"); assert!(e.backtrace().is_some()); } diff --git a/src/extractor.rs b/src/extractor.rs index 4ed88489e..aff15c46d 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -99,13 +99,12 @@ impl Path { impl FromRequest for Path where T: DeserializeOwned, - S: 'static, { type Config = (); type Result = Result; #[inline] - fn from_request(req: &mut HttpRequest, _: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, _: &Self::Config) -> Self::Result { let req = req.clone(); de::Deserialize::deserialize(PathDeserializer::new(&req)) .map_err(|e| e.into()) @@ -167,13 +166,12 @@ impl Query { impl FromRequest for Query where T: de::DeserializeOwned, - S: 'static, { type Config = (); type Result = Result; #[inline] - fn from_request(req: &mut HttpRequest, _: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, _: &Self::Config) -> Self::Result { let req = req.clone(); serde_urlencoded::from_str::(req.query_string()) .map_err(|e| e.into()) @@ -241,7 +239,7 @@ where type Result = Box>; #[inline] - fn from_request(req: &mut HttpRequest, cfg: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result { Box::new( UrlEncoded::new(req.clone()) .limit(cfg.limit) @@ -326,7 +324,7 @@ impl FromRequest for Bytes { type Result = Result>, Error>; #[inline] - fn from_request(req: &mut HttpRequest, cfg: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result { // check content-type cfg.check_mimetype(req)?; @@ -370,7 +368,7 @@ impl FromRequest for String { type Result = Result>, Error>; #[inline] - fn from_request(req: &mut HttpRequest, cfg: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result { // check content-type cfg.check_mimetype(req)?; @@ -471,7 +469,7 @@ mod tests { req.payload_mut() .unread_data(Bytes::from_static(b"hello=world")); - match Bytes::from_request(&mut req, &cfg) + match Bytes::from_request(&req, &cfg) .unwrap() .poll() .unwrap() @@ -490,7 +488,7 @@ mod tests { req.payload_mut() .unread_data(Bytes::from_static(b"hello=world")); - match String::from_request(&mut req, &cfg) + match String::from_request(&req, &cfg) .unwrap() .poll() .unwrap() @@ -514,10 +512,7 @@ mod tests { let mut cfg = FormConfig::default(); cfg.limit(4096); - match Form::::from_request(&mut req, &cfg) - .poll() - .unwrap() - { + match Form::::from_request(&req, &cfg).poll().unwrap() { Async::Ready(s) => { assert_eq!(s.hello, "world"); } @@ -574,29 +569,29 @@ mod tests { let (router, _) = Router::new("", ServerSettings::default(), routes); assert!(router.recognize(&mut req).is_some()); - let s = Path::::from_request(&mut req, &()).unwrap(); + let s = Path::::from_request(&req, &()).unwrap(); assert_eq!(s.key, "name"); assert_eq!(s.value, "user1"); - let s = Path::<(String, String)>::from_request(&mut req, &()).unwrap(); + let s = Path::<(String, String)>::from_request(&req, &()).unwrap(); assert_eq!(s.0, "name"); assert_eq!(s.1, "user1"); - let s = Query::::from_request(&mut req, &()).unwrap(); + let s = Query::::from_request(&req, &()).unwrap(); assert_eq!(s.id, "test"); let mut req = TestRequest::with_uri("/name/32/").finish(); assert!(router.recognize(&mut req).is_some()); - let s = Path::::from_request(&mut req, &()).unwrap(); + let s = Path::::from_request(&req, &()).unwrap(); assert_eq!(s.as_ref().key, "name"); assert_eq!(s.value, 32); - let s = Path::<(String, u8)>::from_request(&mut req, &()).unwrap(); + let s = Path::<(String, u8)>::from_request(&req, &()).unwrap(); assert_eq!(s.0, "name"); assert_eq!(s.1, 32); - let res = Path::>::from_request(&mut req, &()).unwrap(); + let res = Path::>::from_default(&req).unwrap(); assert_eq!(res[0], "name".to_owned()); assert_eq!(res[1], "32".to_owned()); } diff --git a/src/handler.rs b/src/handler.rs index 15f975b6e..216faa29b 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -36,10 +36,7 @@ pub trait Responder { /// Trait implemented by types that can be extracted from request. /// /// Types that implement this trait can be used with `Route::with()` method. -pub trait FromRequest: Sized -where - S: 'static, -{ +pub trait FromRequest: Sized { /// Configuration for conversion process type Config: Default; @@ -47,7 +44,14 @@ where type Result: Into>; /// Convert request to a Self - fn from_request(req: &mut HttpRequest, cfg: &Self::Config) -> Self::Result; + fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result; + + /// Convert request to a Self + /// + /// This method uses default extractor configuration + fn from_default(req: &HttpRequest) -> Self::Result { + Self::from_request(req, &Self::Config::default()) + } } /// Combines two different responder types into a single type @@ -505,12 +509,12 @@ impl Deref for State { } } -impl FromRequest for State { +impl FromRequest for State { type Config = (); type Result = State; #[inline] - fn from_request(req: &mut HttpRequest, _: &Self::Config) -> Self::Result { - State(req.clone()).into() + fn from_request(req: &HttpRequest, _: &Self::Config) -> Self::Result { + State(req.clone()) } } diff --git a/src/httprequest.rs b/src/httprequest.rs index 2225b4bb8..12e5da1d6 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -500,12 +500,12 @@ impl Clone for HttpRequest { } } -impl FromRequest for HttpRequest { +impl FromRequest for HttpRequest { type Config = (); type Result = Self; #[inline] - fn from_request(req: &mut HttpRequest, _: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, _: &Self::Config) -> Self::Result { req.clone() } } diff --git a/src/json.rs b/src/json.rs index 24d1c9c4b..27c99c649 100644 --- a/src/json.rs +++ b/src/json.rs @@ -136,7 +136,7 @@ where type Result = Box>; #[inline] - fn from_request(req: &mut HttpRequest, cfg: &Self::Config) -> Self::Result { + fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result { let req = req.clone(); let err = Rc::clone(&cfg.ehandler); Box::new( diff --git a/src/with.rs b/src/with.rs index bf0d77d2a..e522d97ec 100644 --- a/src/with.rs +++ b/src/with.rs @@ -134,7 +134,7 @@ where let item = if !self.started { self.started = true; - let reply = T::from_request(&mut self.req, self.cfg.as_ref()).into(); + let reply = T::from_request(&self.req, self.cfg.as_ref()).into(); match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -268,7 +268,7 @@ where if !self.started { self.started = true; - let reply = T1::from_request(&mut self.req, self.cfg1.as_ref()).into(); + let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into(); let item1 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -279,7 +279,7 @@ where ReplyItem::None => panic!("use after resolve"), }; - let reply = T2::from_request(&mut self.req, self.cfg2.as_ref()).into(); + let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let item2 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -309,8 +309,7 @@ where if self.fut1.is_some() { match self.fut1.as_mut().unwrap().poll()? { Async::Ready(item) => { - let reply = - T2::from_request(&mut self.req, self.cfg2.as_ref()).into(); + let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let item2 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -480,7 +479,7 @@ where if !self.started { self.started = true; - let reply = T1::from_request(&mut self.req, self.cfg1.as_ref()).into(); + let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into(); let item1 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -491,7 +490,7 @@ where ReplyItem::None => panic!("use after resolve"), }; - let reply = T2::from_request(&mut self.req, self.cfg2.as_ref()).into(); + let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let item2 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -503,7 +502,7 @@ where ReplyItem::None => panic!("use after resolve"), }; - let reply = T3::from_request(&mut self.req, self.cfg3.as_ref()).into(); + let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into(); let item3 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -536,8 +535,7 @@ where Async::Ready(item) => { self.item1 = Some(item); self.fut1.take(); - let reply = - T2::from_request(&mut self.req, self.cfg2.as_ref()).into(); + let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let item2 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -548,8 +546,7 @@ where ReplyItem::None => panic!("use after resolve"), }; - let reply = - T3::from_request(&mut self.req, self.cfg3.as_ref()).into(); + let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into(); let item3 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg, @@ -584,8 +581,7 @@ where match self.fut2.as_mut().unwrap().poll()? { Async::Ready(item) => { self.fut2.take(); - let reply = - T3::from_request(&mut self.req, self.cfg3.as_ref()).into(); + let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into(); let item3 = match reply.into() { ReplyItem::Error(err) => return Err(err), ReplyItem::Message(msg) => msg,