From 8791c0f8802852d5677d12336fc704cd1750593b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 31 Mar 2018 09:58:33 -0700 Subject: [PATCH] simplify With handlers --- src/lib.rs | 1 - src/resource.rs | 6 +- src/route.rs | 13 +++-- src/with.rs | 146 +++++++++++++++++------------------------------- 4 files changed, 62 insertions(+), 104 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 13c45fbd9..555f610fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -178,7 +178,6 @@ pub mod dev { pub use handler::{Handler, Reply, FromRequest}; pub use route::Route; pub use resource::Resource; - pub use with::WithHandler; pub use json::JsonBody; pub use router::{Router, Pattern}; pub use param::{FromParam, Params}; diff --git a/src/resource.rs b/src/resource.rs index dd3768885..da74f4e0e 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -10,7 +10,6 @@ use handler::{Reply, Handler, Responder, FromRequest}; use middleware::Middleware; use httprequest::HttpRequest; use httpresponse::HttpResponse; -use with::WithHandler; /// *Resource* is an entry in route table which corresponds to requested URL. /// @@ -139,8 +138,9 @@ impl Resource { /// ```rust,ignore /// Resource::resource("/", |r| r.route().with(index) /// ``` - pub fn with(&mut self, handler: H) - where H: WithHandler, + pub fn with(&mut self, handler: F) + where F: Fn(T) -> R + 'static, + R: Responder + 'static, T: FromRequest + 'static, { self.routes.push(Route::default()); diff --git a/src/route.rs b/src/route.rs index 863a4ab55..1eebaa3ea 100644 --- a/src/route.rs +++ b/src/route.rs @@ -11,7 +11,7 @@ use handler::{Reply, ReplyItem, Handler, FromRequest, use middleware::{Middleware, Response as MiddlewareResponse, Started as MiddlewareStarted}; use httprequest::HttpRequest; use httpresponse::HttpResponse; -use with::{with, with2, with3, WithHandler}; +use with::{With, With2, With3}; /// Resource route definition /// @@ -127,11 +127,12 @@ impl Route { /// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor /// } /// ``` - pub fn with(&mut self, handler: H) - where H: WithHandler, + pub fn with(&mut self, handler: F) + where F: Fn(T) -> R + 'static, + R: Responder + 'static, T: FromRequest + 'static, { - self.h(with(handler)) + self.h(With::new(handler)) } /// Set handler function, function has to accept two request extractors. @@ -170,7 +171,7 @@ impl Route { T1: FromRequest + 'static, T2: FromRequest + 'static, { - self.h(with2(handler)) + self.h(With2::new(handler)) } /// Set handler function, function has to accept three request extractors. @@ -181,7 +182,7 @@ impl Route { T2: FromRequest + 'static, T3: FromRequest + 'static, { - self.h(with3(handler)) + self.h(With3::new(handler)) } } diff --git a/src/with.rs b/src/with.rs index ea73eaafc..2a4420392 100644 --- a/src/with.rs +++ b/src/with.rs @@ -8,55 +8,27 @@ use handler::{Handler, FromRequest, Reply, ReplyItem, Responder}; use httprequest::HttpRequest; use httpresponse::HttpResponse; - -/// Trait defines object that could be registered as route handler -#[allow(unused_variables)] -pub trait WithHandler: 'static - where T: FromRequest, S: 'static +pub struct With + where F: Fn(T) -> R { - /// The type of value that handler will return. - type Result: Responder; - - /// Handle request - fn handle(&mut self, data: T) -> Self::Result; -} - -/// WithHandler for Fn() -impl WithHandler for F - where F: Fn(T) -> R + 'static, - R: Responder + 'static, - T: FromRequest, - S: 'static, -{ - type Result = R; - - fn handle(&mut self, item: T) -> R { - (self)(item) - } -} - -pub(crate) -fn with(h: H) -> With - where H: WithHandler, - T: FromRequest, -{ - With{hnd: Rc::new(UnsafeCell::new(h)), _t: PhantomData, _s: PhantomData} -} - -pub struct With - where H: WithHandler + 'static, - T: FromRequest, - S: 'static, -{ - hnd: Rc>, + hnd: Rc>, _t: PhantomData, _s: PhantomData, } -impl Handler for With - where H: WithHandler, +impl With + where F: Fn(T) -> R, +{ + pub fn new(f: F) -> Self { + With{hnd: Rc::new(UnsafeCell::new(f)), _t: PhantomData, _s: PhantomData} + } +} + +impl Handler for With + where F: Fn(T) -> R + 'static, + R: Responder + 'static, T: FromRequest + 'static, - S: 'static, H: 'static + S: 'static { type Result = Reply; @@ -77,20 +49,22 @@ impl Handler for With } } -struct WithHandlerFut - where H: WithHandler, - T: FromRequest, - T: 'static, S: 'static +struct WithHandlerFut + where F: Fn(T) -> R, + R: Responder, + T: FromRequest + 'static, + S: 'static { started: bool, - hnd: Rc>, + hnd: Rc>, req: HttpRequest, fut1: Option>>, fut2: Option>>, } -impl Future for WithHandlerFut - where H: WithHandler, +impl Future for WithHandlerFut + where F: Fn(T) -> R, + R: Responder + 'static, T: FromRequest + 'static, S: 'static { @@ -120,38 +94,23 @@ impl Future for WithHandlerFut } }; - let hnd: &mut H = unsafe{&mut *self.hnd.get()}; - let item = match hnd.handle(item).respond_to(self.req.without_state()) { + let hnd: &mut F = unsafe{&mut *self.hnd.get()}; + let item = match (*hnd)(item).respond_to(self.req.without_state()) { Ok(item) => item.into(), - Err(err) => return Err(err.into()), + Err(e) => return Err(e.into()), }; match item.into() { - ReplyItem::Message(resp) => return Ok(Async::Ready(resp)), - ReplyItem::Future(fut) => self.fut2 = Some(fut), + ReplyItem::Message(resp) => Ok(Async::Ready(resp)), + ReplyItem::Future(fut) => { + self.fut2 = Some(fut); + self.poll() + } } - - self.poll() } } -pub(crate) -fn with2(h: F) -> With2 - where F: Fn(T1, T2) -> R, - R: Responder, - T1: FromRequest, - T2: FromRequest, -{ - With2{hnd: Rc::new(UnsafeCell::new(h)), - _t1: PhantomData, _t2: PhantomData, _s: PhantomData} -} - -pub struct With2 - where F: Fn(T1, T2) -> R, - R: Responder, - T1: FromRequest, - T2: FromRequest, - S: 'static, +pub struct With2 where F: Fn(T1, T2) -> R { hnd: Rc>, _t1: PhantomData, @@ -159,6 +118,14 @@ pub struct With2 _s: PhantomData, } +impl With2 where F: Fn(T1, T2) -> R +{ + pub fn new(f: F) -> Self { + With2{hnd: Rc::new(UnsafeCell::new(f)), + _t1: PhantomData, _t2: PhantomData, _s: PhantomData} + } +} + impl Handler for With2 where F: Fn(T1, T2) -> R + 'static, R: Responder + 'static, @@ -289,26 +256,7 @@ impl Future for WithHandlerFut2 } } -pub(crate) -fn with3(h: F) -> With3 - where F: Fn(T1, T2, T3) -> R + 'static, - R: Responder, - T1: FromRequest, - T2: FromRequest, - T3: FromRequest, -{ - With3{hnd: Rc::new(UnsafeCell::new(h)), - _s: PhantomData, _t1: PhantomData, _t2: PhantomData, _t3: PhantomData} -} - -pub struct With3 - where F: Fn(T1, T2, T3) -> R + 'static, - R: Responder + 'static, - T1: FromRequest, - T2: FromRequest, - T3: FromRequest, - S: 'static, -{ +pub struct With3 where F: Fn(T1, T2, T3) -> R { hnd: Rc>, _t1: PhantomData, _t2: PhantomData, @@ -316,6 +264,16 @@ pub struct With3 _s: PhantomData, } + +impl With3 + where F: Fn(T1, T2, T3) -> R, +{ + pub fn new(f: F) -> Self { + With3{hnd: Rc::new(UnsafeCell::new(f)), + _s: PhantomData, _t1: PhantomData, _t2: PhantomData, _t3: PhantomData} + } +} + impl Handler for With3 where F: Fn(T1, T2, T3) -> R + 'static, R: Responder + 'static,