From a8ae9390190962fe20f8c7252a0ba8939aaf33aa Mon Sep 17 00:00:00 2001 From: Dominic Date: Tue, 14 Apr 2020 22:40:27 +0200 Subject: [PATCH] clean up async shit --- gotham_restful/src/routing.rs | 69 ++++++++++------------------------- 1 file changed, 19 insertions(+), 50 deletions(-) diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index 8aac5ea..ee7cf31 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -7,7 +7,6 @@ use crate::{ #[cfg(feature = "openapi")] use crate::OpenapiRouter; -use futures_core::future::Future; use futures_util::{future, future::FutureExt}; use gotham::{ handler::{HandlerError, HandlerFuture, IntoHandlerError, IntoHandlerFuture}, @@ -66,17 +65,11 @@ pub trait DrawResources /// `Resource::setup` method. pub trait DrawResourceRoutes { - fn read_all(&mut self) - where - dyn Future::Err>> : Send; + fn read_all(&mut self); - fn read(&mut self) - where - dyn Future::Err>> : Send; + fn read(&mut self); - fn search(&mut self) - where - dyn Future::Err>> : Send; + fn search(&mut self); fn create(&mut self) where @@ -93,13 +86,9 @@ pub trait DrawResourceRoutes Handler::Res : Send + 'static, Handler::Body : 'static; - fn delete_all(&mut self) - where - dyn Future::Err>> : Send; + fn delete_all(&mut self); - fn delete(&mut self) - where - dyn Future::Err>> : Send; + fn delete(&mut self); } fn response_from(res : Response, state : &State) -> hyper::Response @@ -119,8 +108,7 @@ fn response_from(res : Response, state : &State) -> hyper::Response fn to_handler_future(mut state : State, get_result : F) -> Pin> where F : FnOnce(&mut State) -> R, - R : ResourceResult, - dyn Future> : Send + R : ResourceResult { get_result(&mut state).into_response() .then(|res| @@ -134,7 +122,7 @@ where ).boxed() } -async fn body_to_res(state : &mut State, get_result : F) -> Result, HandlerError> +async fn body_to_res(mut state : State, get_result : F) -> (State, Result, HandlerError>) where B : RequestBody, F : FnOnce(&mut State, B) -> R, @@ -144,52 +132,53 @@ where let body = match body { Ok(body) => body, - Err(e) => return Err(e.into_handler_error()) + Err(e) => return (state, Err(e.into_handler_error())) }; let content_type : Mime = match HeaderMap::borrow_from(&state).get(CONTENT_TYPE) { Some(content_type) => content_type.to_str().unwrap().parse().unwrap(), None => { let res = create_empty_response(&state, StatusCode::UNSUPPORTED_MEDIA_TYPE); - return Ok(res) + return (state, Ok(res)) } }; let res = { let body = match B::from_body(body, content_type) { Ok(body) => body, - Err(e) => return { + Err(e) => { let error : ResourceError = e.into(); - match serde_json::to_string(&error) { + let res = match serde_json::to_string(&error) { Ok(json) => { let res = create_response(&state, StatusCode::BAD_REQUEST, APPLICATION_JSON, json); Ok(res) }, Err(e) => Err(e.into_handler_error()) - } + }; + return (state, res) } }; get_result(&mut state, body) }; - let res = res.into_response().await; - match res { + let res = match res.into_response().await { Ok(res) => { let r = response_from(res, &state); Ok(r) }, Err(e) => Err(e.into_handler_error()) - } + }; + (state, res) } -fn handle_with_body(mut state : State, get_result : F) -> Pin> +fn handle_with_body(state : State, get_result : F) -> Pin> where B : RequestBody + 'static, F : FnOnce(&mut State, B) -> R + Send + 'static, R : ResourceResult + Send + 'static { - body_to_res(&mut state, get_result) - .then(|res| match res { + body_to_res(state, get_result) + .then(|(state, res)| match res { Ok(ok) => future::ok((state, ok)), Err(err) => future::err((state, err)) }) @@ -197,15 +186,11 @@ where } fn read_all_handler(state : State) -> Pin> -where - dyn Future::Err>> : Send { to_handler_future(state, |state| Handler::read_all(state)) } fn read_handler(state : State) -> Pin> -where - dyn Future::Err>> : Send { let id = { let path : &PathExtractor = PathExtractor::borrow_from(&state); @@ -215,8 +200,6 @@ where } fn search_handler(mut state : State) -> Pin> -where - dyn Future::Err>> : Send { let query = Handler::Query::take_from(&mut state); to_handler_future(state, |state| Handler::search(state, query)) @@ -251,15 +234,11 @@ where } fn delete_all_handler(state : State) -> Pin> -where - dyn Future::Err>> : Send { to_handler_future(state, |state| Handler::delete_all(state)) } fn delete_handler(state : State) -> Pin> -where - dyn Future::Err>> : Send { let id = { let path : &PathExtractor = PathExtractor::borrow_from(&state); @@ -358,8 +337,6 @@ macro_rules! implDrawResourceRoutes { P : RefUnwindSafe + Send + Sync + 'static { fn read_all(&mut self) - where - dyn Future::Err>> : Send { let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); self.0.get(&self.1) @@ -368,8 +345,6 @@ macro_rules! implDrawResourceRoutes { } fn read(&mut self) - where - dyn Future::Err>> : Send { let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); self.0.get(&format!("{}/:id", self.1)) @@ -379,8 +354,6 @@ macro_rules! implDrawResourceRoutes { } fn search(&mut self) - where - dyn Future::Err>> : Send { let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); self.0.get(&format!("{}/search", self.1)) @@ -430,8 +403,6 @@ macro_rules! implDrawResourceRoutes { } fn delete_all(&mut self) - where - dyn Future::Err>> : Send { let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); self.0.delete(&self.1) @@ -440,8 +411,6 @@ macro_rules! implDrawResourceRoutes { } fn delete(&mut self) - where - dyn Future::Err>> : Send { let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into(); self.0.delete(&format!("{}/:id", self.1))