1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

asyncify method proc macro

This commit is contained in:
Dominic 2020-04-15 21:07:33 +02:00
parent 427c836f52
commit 89f6494b51
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
3 changed files with 23 additions and 25 deletions

View file

@ -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<Box<dyn Future<Output = Self::Res> + Send>>;
fn read_all(state : State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + 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<Box<dyn Future<Output = Self::Res> + Send>>;
fn read(state : State, id : Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
}
/// Handle a GET request on the Resource with additional search parameters.
@ -58,7 +58,7 @@ pub trait ResourceSearch : ResourceMethod
{
type Query : ResourceType + QueryStringExtractor<Body> + Sync;
fn search(state : &mut State, query : Self::Query) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
fn search(state : State, query : Self::Query) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + 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<Box<dyn Future<Output = Self::Res> + Send>>;
fn create(state : State, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + 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<Box<dyn Future<Output = Self::Res> + Send>>;
fn update_all(state : State, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + 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<Box<dyn Future<Output = Self::Res> + Send>>;
fn update(state : State, id : Self::ID, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
}
/// Handle a DELETE request on the Resource root.
pub trait ResourceDeleteAll : ResourceMethod
{
fn delete_all(state : &mut State) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
fn delete_all(state : State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + 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<Box<dyn Future<Output = Self::Res> + Send>>;
fn delete(state : State, id : Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
}

View file

@ -106,12 +106,12 @@ fn response_from(res : Response, state : &State) -> hyper::Response<Body>
r
}
async fn to_handler_future<F, R>(mut state : State, get_result : F) -> Result<(State, gotham::hyper::Response<Body>), (State, HandlerError)>
async fn to_handler_future<F, R>(state : State, get_result : F) -> Result<(State, gotham::hyper::Response<Body>), (State, HandlerError)>
where
F : FnOnce(&mut State) -> Pin<Box<dyn Future<Output = R> + Send>>,
F : FnOnce(State) -> Pin<Box<dyn Future<Output = (State, R)> + 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<B, F, R>(mut state : State, get_result : F) -> (State, Result<gotham::hyper::Response<Body>, HandlerError>)
where
B : RequestBody,
F : FnOnce(&mut State, B) -> Pin<Box<dyn Future<Output = R> + Send>>,
F : FnOnce(State, B) -> Pin<Box<dyn Future<Output = (State, R)> + 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<B, F, R>(state : State, get_result : F) -> Pin<Box<HandlerFuture>>
where
B : RequestBody + 'static,
F : FnOnce(&mut State, B) -> Pin<Box<dyn Future<Output = R> + Send>> + Send + 'static,
F : FnOnce(State, B) -> Pin<Box<dyn Future<Output = (State, R)> + Send>> + Send + 'static,
R : ResourceResult + Send + 'static
{
body_to_res(state, get_result)