diff --git a/gotham_restful/src/resource.rs b/gotham_restful/src/resource.rs index 79b0de9..39bd964 100644 --- a/gotham_restful/src/resource.rs +++ b/gotham_restful/src/resource.rs @@ -42,7 +42,7 @@ pub trait ResourceMethod /// Handle a GET request on the Resource root. pub trait ResourceReadAll : ResourceMethod { - fn read_all(state : &mut State) -> Pin + Send>>; + fn read_all(state : State) -> Pin + Send>>; } /// Handle a GET request on the Resource with an id. @@ -50,7 +50,7 @@ pub trait ResourceRead : ResourceMethod { type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static; - fn read(state : &mut State, id : Self::ID) -> Pin + Send>>; + fn read(state : State, id : Self::ID) -> Pin + Send>>; } /// Handle a GET request on the Resource with additional search parameters. @@ -58,7 +58,7 @@ pub trait ResourceSearch : ResourceMethod { type Query : ResourceType + QueryStringExtractor + Sync; - fn search(state : &mut State, query : Self::Query) -> Pin + Send>>; + fn search(state : State, query : Self::Query) -> Pin + Send>>; } /// Handle a POST request on the Resource root. @@ -66,7 +66,7 @@ pub trait ResourceCreate : ResourceMethod { type Body : RequestBody; - fn create(state : &mut State, body : Self::Body) -> Pin + Send>>; + fn create(state : State, body : Self::Body) -> Pin + Send>>; } /// Handle a PUT request on the Resource root. @@ -74,7 +74,7 @@ pub trait ResourceUpdateAll : ResourceMethod { type Body : RequestBody; - fn update_all(state : &mut State, body : Self::Body) -> Pin + Send>>; + fn update_all(state : State, body : Self::Body) -> Pin + Send>>; } /// Handle a PUT request on the Resource with an id. @@ -83,13 +83,13 @@ pub trait ResourceUpdate : ResourceMethod type Body : RequestBody; type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static; - fn update(state : &mut State, id : Self::ID, body : Self::Body) -> Pin + Send>>; + fn update(state : State, id : Self::ID, body : Self::Body) -> Pin + Send>>; } /// Handle a DELETE request on the Resource root. pub trait ResourceDeleteAll : ResourceMethod { - fn delete_all(state : &mut State) -> Pin + Send>>; + fn delete_all(state : State) -> Pin + Send>>; } /// Handle a DELETE request on the Resource with an id. @@ -97,5 +97,5 @@ pub trait ResourceDelete : ResourceMethod { type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static; - fn delete(state : &mut State, id : Self::ID) -> Pin + Send>>; + fn delete(state : State, id : Self::ID) -> Pin + Send>>; } diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index 7fd66f0..13672c2 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -106,12 +106,12 @@ fn response_from(res : Response, state : &State) -> hyper::Response r } -async fn to_handler_future(mut state : State, get_result : F) -> Result<(State, gotham::hyper::Response), (State, HandlerError)> +async fn to_handler_future(state : State, get_result : F) -> Result<(State, gotham::hyper::Response), (State, HandlerError)> where - F : FnOnce(&mut State) -> Pin + Send>>, + F : FnOnce(State) -> Pin + Send>>, R : ResourceResult { - let res = get_result(&mut state).await; + let (state, res) = get_result(state).await; let res = res.into_response().await; match res { Ok(res) => { @@ -125,7 +125,7 @@ where async fn body_to_res(mut state : State, get_result : F) -> (State, Result, HandlerError>) where B : RequestBody, - F : FnOnce(&mut State, B) -> Pin + Send>>, + F : FnOnce(State, B) -> Pin + Send>>, R : ResourceResult { let body = to_bytes(Body::take_from(&mut state)).await; @@ -158,10 +158,10 @@ where return (state, res) } }; - get_result(&mut state, body) + get_result(state, body) }; - let res = res.await; + let (state, res) = res.await; let res = res.into_response().await; let res = match res { @@ -177,7 +177,7 @@ where fn handle_with_body(state : State, get_result : F) -> Pin> where B : RequestBody + 'static, - F : FnOnce(&mut State, B) -> Pin + Send>> + Send + 'static, + F : FnOnce(State, B) -> Pin + Send>> + Send + 'static, R : ResourceResult + Send + 'static { body_to_res(state, get_result) diff --git a/gotham_restful_derive/src/method.rs b/gotham_restful_derive/src/method.rs index 9ca87c6..fb89b45 100644 --- a/gotham_restful_derive/src/method.rs +++ b/gotham_restful_derive/src/method.rs @@ -325,6 +325,7 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result Result = args.iter().map(|arg| match (&arg.ty, &arg.ident) { - (MethodArgumentType::StateRef, _) => quote!(#state_ident), - (MethodArgumentType::StateMutRef, _) => quote!(#state_ident), + (MethodArgumentType::StateRef, _) => quote!(&#state_ident), + (MethodArgumentType::StateMutRef, _) => quote!(&mut #state_ident), (MethodArgumentType::MethodArg(_), ident) => quote!(#ident), (MethodArgumentType::DatabaseConnection(_), _) => quote!(&#conn_ident), (MethodArgumentType::AuthStatus(_), _) => quote!(#auth_ident), @@ -407,7 +408,7 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result::borrow_from(#state_ident).clone(); + let #auth_ident : #auth_ty = <#auth_ty>::borrow_from(&#state_ident).clone(); }; } @@ -430,10 +431,6 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result Result Pin + Send>> + fn #method_ident(#(#args_def),*) -> std::pin::Pin + Send>> { #[allow(unused_imports)] use #krate::export::{FromState, FutureExt}; @@ -458,7 +455,8 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result