From dcc5eb7ace6a13e5bed78163308eed8de01c2bfc Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 26 Mar 2018 23:34:31 -0700 Subject: [PATCH] pass request as value --- examples/json/src/main.rs | 2 +- src/extractor.rs | 4 ++-- src/json.rs | 2 +- src/route.rs | 2 +- src/with.rs | 9 +++++---- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index 5d074f93..6d85a35e 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -31,7 +31,7 @@ fn index(req: HttpRequest) -> Box> { } /// This handler uses `With` helper for loading serde json object. -fn extract_item(_: &HttpRequest, item: Json) -> Result { +fn extract_item(_: HttpRequest, item: Json) -> Result { println!("model: {:?}", &item); httpcodes::HTTPOk.build().json(item.0) // <- send response } diff --git a/src/extractor.rs b/src/extractor.rs index c7eb558f..a6373ab3 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -33,7 +33,7 @@ pub trait HttpRequestExtractor: Sized where T: DeserializeOwned /// } /// /// /// extract path info using serde -/// fn index(req: &HttpRequest, info: Path) -> Result { +/// fn index(req: HttpRequest, info: Path) -> Result { /// Ok(format!("Welcome {}!", info.username)) /// } /// @@ -90,7 +90,7 @@ impl HttpRequestExtractor for Path where T: DeserializeOwned /// /// // use `with` extractor for query info /// // this handler get called only if request's query contains `username` field -/// fn index(req: &HttpRequest, info: Query) -> Result { +/// fn index(req: HttpRequest, info: Query) -> Result { /// Ok(format!("Welcome {}!", info.username)) /// } /// diff --git a/src/json.rs b/src/json.rs index 5687e75c..94a3f368 100644 --- a/src/json.rs +++ b/src/json.rs @@ -251,7 +251,7 @@ mod tests { #[test] fn test_with_json() { - let mut handler = with(|_: &_, data: Json| data); + let mut handler = with(|_: _, data: Json| data); let req = HttpRequest::default(); let mut json = handler.handle(req).into_future(); diff --git a/src/route.rs b/src/route.rs index 83879bce..9f18cce5 100644 --- a/src/route.rs +++ b/src/route.rs @@ -127,7 +127,7 @@ impl Route { /// } /// /// /// extract path info using serde - /// fn index(req: &HttpRequest, info: Path) -> Result { + /// fn index(req: HttpRequest, info: Path) -> Result { /// Ok(format!("Welcome {}!", info.username)) /// } /// diff --git a/src/with.rs b/src/with.rs index b0cb0290..a7b338a7 100644 --- a/src/with.rs +++ b/src/with.rs @@ -20,19 +20,19 @@ pub trait WithHandler: 'static type Result: Responder; /// Handle request - fn handle(&mut self, req: &HttpRequest, data: D) -> Self::Result; + fn handle(&mut self, req: HttpRequest, data: D) -> Self::Result; } /// WithHandler for Fn() impl WithHandler for F - where F: Fn(&HttpRequest, D) -> R + 'static, + where F: Fn(HttpRequest, D) -> R + 'static, R: Responder + 'static, D: HttpRequestExtractor, T: DeserializeOwned, { type Result = R; - fn handle(&mut self, req: &HttpRequest, item: D) -> R { + fn handle(&mut self, req: HttpRequest, item: D) -> R { (self)(req, item) } } @@ -114,7 +114,8 @@ impl Future for WithHandlerFut }; let hnd: &mut H = unsafe{&mut *self.hnd.get()}; - let item = match hnd.handle(&self.req, item).respond_to(self.req.without_state()) + let item = match hnd.handle(self.req.clone(), item) + .respond_to(self.req.without_state()) { Ok(item) => item.into(), Err(err) => return Err(err.into()),