From 53c5151692978ecf21dda31b5c93cb1469e5c36f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 21 Nov 2019 15:01:34 +0600 Subject: [PATCH] use response instead of result for asyn c handlers --- examples/basic.rs | 6 ++--- src/handler.rs | 63 +++++++++++++++++++---------------------------- src/resource.rs | 9 +++---- src/route.rs | 15 ++++++----- src/web.rs | 9 +++---- 5 files changed, 43 insertions(+), 59 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 76c977322..d25db7895 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -8,9 +8,9 @@ fn index(req: HttpRequest, name: web::Path) -> String { format!("Hello: {}!\r\n", name) } -async fn index_async(req: HttpRequest) -> Result<&'static str, Error> { +async fn index_async(req: HttpRequest) -> &'static str { println!("REQ: {:?}", req); - Ok("Hello world!\r\n") + "Hello world!\r\n" } #[get("/")] @@ -26,7 +26,7 @@ fn main() -> std::io::Result<()> { App::new() .wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2")) .wrap(middleware::Compress::default()) - // .wrap(middleware::Logger::default()) + .wrap(middleware::Logger::default()) .service(index) .service(no_params) .service( diff --git a/src/handler.rs b/src/handler.rs index 7f5d52945..767f630da 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -119,21 +119,19 @@ impl Future for HandlerServiceResponse { } /// Async handler converter factory -pub trait AsyncFactory: Clone + 'static +pub trait AsyncFactory: Clone + 'static where - R: Future>, + R: Future, O: Responder, - E: Into, { fn call(&self, param: T) -> R; } -impl AsyncFactory<(), R, O, E> for F +impl AsyncFactory<(), R, O> for F where F: Fn() -> R + Clone + 'static, - R: Future>, + R: Future, O: Responder, - E: Into, { fn call(&self, _: ()) -> R { (self)() @@ -141,23 +139,21 @@ where } #[doc(hidden)] -pub struct AsyncHandler +pub struct AsyncHandler where - F: AsyncFactory, - R: Future>, + F: AsyncFactory, + R: Future, O: Responder, - E: Into, { hnd: F, - _t: PhantomData<(T, R, O, E)>, + _t: PhantomData<(T, R, O)>, } -impl AsyncHandler +impl AsyncHandler where - F: AsyncFactory, - R: Future>, + F: AsyncFactory, + R: Future, O: Responder, - E: Into, { pub fn new(hnd: F) -> Self { AsyncHandler { @@ -167,12 +163,11 @@ where } } -impl Clone for AsyncHandler +impl Clone for AsyncHandler where - F: AsyncFactory, - R: Future>, + F: AsyncFactory, + R: Future, O: Responder, - E: Into, { fn clone(&self) -> Self { AsyncHandler { @@ -182,17 +177,16 @@ where } } -impl Service for AsyncHandler +impl Service for AsyncHandler where - F: AsyncFactory, - R: Future>, + F: AsyncFactory, + R: Future, O: Responder, - E: Into, { type Request = (T, HttpRequest); type Response = ServiceResponse; type Error = Infallible; - type Future = AsyncHandlerServiceResponse; + type Future = AsyncHandlerServiceResponse; fn poll_ready(&mut self, _: &mut Context) -> Poll> { Poll::Ready(Ok(())) @@ -209,11 +203,10 @@ where #[doc(hidden)] #[pin_project] -pub struct AsyncHandlerServiceResponse +pub struct AsyncHandlerServiceResponse where - T: Future>, + T: Future, R: Responder, - E: Into, { #[pin] fut: T, @@ -222,11 +215,10 @@ where req: Option, } -impl Future for AsyncHandlerServiceResponse +impl Future for AsyncHandlerServiceResponse where - T: Future>, + T: Future, R: Responder, - E: Into, { type Output = Result; @@ -247,16 +239,12 @@ where } match this.fut.poll(cx) { - Poll::Ready(Ok(res)) => { + Poll::Ready(res) => { let fut = res.respond_to(this.req.as_ref().unwrap()); self.as_mut().project().fut2.set(Some(fut)); self.poll(cx) } Poll::Pending => Poll::Pending, - Poll::Ready(Err(e)) => { - let res: Response = e.into().into(); - Poll::Ready(Ok(ServiceResponse::new(this.req.take().unwrap(), res))) - } } } } @@ -387,11 +375,10 @@ macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => { } } - impl AsyncFactory<($($T,)+), Res, O, E1> for Func + impl AsyncFactory<($($T,)+), Res, O> for Func where Func: Fn($($T,)+) -> Res + Clone + 'static, - Res: Future>, + Res: Future, O: Responder, - E1: Into, { fn call(&self, param: ($($T,)+)) -> Res { (self)($(param.$n,)+) diff --git a/src/resource.rs b/src/resource.rs index 553d41568..904bc124f 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -264,13 +264,12 @@ where /// App::new().service(web::resource("/").route(web::route().to_async(index))); /// ``` #[allow(clippy::wrong_self_convention)] - pub fn to_async(mut self, handler: F) -> Self + pub fn to_async(mut self, handler: F) -> Self where - F: AsyncFactory, + F: AsyncFactory, I: FromRequest + 'static, - R: Future> + 'static, - O: Responder + 'static, - E: Into + 'static, + R: Future + 'static, + U: Responder + 'static, { self.routes.push(Route::new().to_async(handler)); self diff --git a/src/route.rs b/src/route.rs index fb46dbfd2..9b2c4390c 100644 --- a/src/route.rs +++ b/src/route.rs @@ -261,13 +261,12 @@ impl Route { /// } /// ``` #[allow(clippy::wrong_self_convention)] - pub fn to_async(mut self, handler: F) -> Self + pub fn to_async(mut self, handler: F) -> Self where - F: AsyncFactory, + F: AsyncFactory, T: FromRequest + 'static, - R: Future> + 'static, - O: Responder + 'static, - E: Into + 'static, + R: Future + 'static, + U: Responder + 'static, { self.service = Box::new(RouteNewService::new(Extract::new(AsyncHandler::new( handler, @@ -410,7 +409,7 @@ mod tests { .route(web::post().to_async(|| { async { delay_for(Duration::from_millis(100)).await; - Ok::<_, Error>(HttpResponse::Created()) + HttpResponse::Created() } })) .route(web::delete().to_async(|| { @@ -423,9 +422,9 @@ mod tests { .service(web::resource("/json").route(web::get().to_async(|| { async { delay_for(Duration::from_millis(25)).await; - Ok::<_, Error>(web::Json(MyObject { + web::Json(MyObject { name: "test".to_string(), - })) + }) } }))), ) diff --git a/src/web.rs b/src/web.rs index 67cfd51a2..3d716dc23 100644 --- a/src/web.rs +++ b/src/web.rs @@ -265,13 +265,12 @@ where /// web::to_async(index)) /// ); /// ``` -pub fn to_async(handler: F) -> Route +pub fn to_async(handler: F) -> Route where - F: AsyncFactory, + F: AsyncFactory, I: FromRequest + 'static, - R: Future> + 'static, - O: Responder + 'static, - E: Into + 'static, + R: Future + 'static, + U: Responder + 'static, { Route::new().to_async(handler) }