From f2bcc8438f9b4c8d6c5f117ddc933be9b0998631 Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 18 Jan 2021 19:04:06 +0100 Subject: [PATCH] fix implicit `&'static mut State` error --- Cargo.toml | 1 + derive/src/endpoint.rs | 6 +++--- src/endpoint.rs | 12 ++++++------ tests/async_methods.rs | 19 +++++++++++++++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 65d8f49..9203f31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ diesel = { version = "1.4.4", features = ["postgres"] } futures-executor = "0.3.5" paste = "1.0" pretty_env_logger = "0.4" +tokio = { version = "0.2", features = ["time"], default-features = false } thiserror = "1.0.18" trybuild = "1.0.27" diff --git a/derive/src/endpoint.rs b/derive/src/endpoint.rs index 87ac3d8..a363ec0 100644 --- a/derive/src/endpoint.rs +++ b/derive/src/endpoint.rs @@ -554,12 +554,12 @@ fn expand_endpoint_type(mut ty: EndpointType, attrs: AttributeArgs, fun: &ItemFn } type Body = #body_ty; - fn handle( - state: &mut ::gotham_restful::gotham::state::State, + fn handle<'a>( + state: &'a mut ::gotham_restful::gotham::state::State, placeholders: Self::Placeholders, params: Self::Params, body: ::std::option::Option - ) -> ::gotham_restful::export::BoxFuture<'static, Self::Output> { + ) -> ::gotham_restful::export::BoxFuture<'a, Self::Output> { #handle_content } diff --git a/src/endpoint.rs b/src/endpoint.rs index 90bc952..32dd015 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -56,12 +56,12 @@ pub trait Endpoint { } /// The handler for this endpoint. - fn handle( - state: &mut State, + fn handle<'a>( + state: &'a mut State, placeholders: Self::Placeholders, params: Self::Params, body: Option - ) -> BoxFuture<'static, Self::Output>; + ) -> BoxFuture<'a, Self::Output>; } #[cfg(feature = "openapi")] @@ -94,12 +94,12 @@ impl Endpoint for E { E::wants_auth() } - fn handle( - state: &mut State, + fn handle<'a>( + state: &'a mut State, placeholders: Self::Placeholders, params: Self::Params, body: Option - ) -> BoxFuture<'static, Self::Output> { + ) -> BoxFuture<'a, Self::Output> { E::handle(state, placeholders, params, body) } } diff --git a/tests/async_methods.rs b/tests/async_methods.rs index be6a406..21c3462 100644 --- a/tests/async_methods.rs +++ b/tests/async_methods.rs @@ -1,10 +1,15 @@ #[macro_use] extern crate gotham_derive; -use gotham::{router::builder::*, test::TestServer}; +use gotham::{ + hyper::{HeaderMap, Method}, + router::builder::*, + test::TestServer +}; use gotham_restful::*; use mime::{APPLICATION_JSON, TEXT_PLAIN}; use serde::Deserialize; +use tokio::time::{delay_for, Duration}; mod util { include!("util/mod.rs"); @@ -12,7 +17,7 @@ mod util { use util::{test_delete_response, test_get_response, test_post_response, test_put_response}; #[derive(Resource)] -#[resource(read_all, read, search, create, change_all, change, remove_all, remove)] +#[resource(read_all, read, search, create, change_all, change, remove_all, remove, state_test)] struct FooResource; #[derive(Deserialize)] @@ -77,6 +82,15 @@ async fn remove(_id: u64) -> Raw<&'static [u8]> { Raw::new(REMOVE_RESPONSE, TEXT_PLAIN) } +const STATE_TEST_RESPONSE: &[u8] = b"xxJbxOuwioqR5DfzPuVqvaqRSfpdNQGluIvHU4n1LM"; +#[endpoint(method = "Method::GET", uri = "state_test")] +async fn state_test(state: &mut State) -> Raw<&'static [u8]> { + delay_for(Duration::from_nanos(1)).await; + state.borrow::(); + delay_for(Duration::from_nanos(1)).await; + Raw::new(STATE_TEST_RESPONSE, TEXT_PLAIN) +} + #[test] fn async_methods() { let _ = pretty_env_logger::try_init_timed(); @@ -112,4 +126,5 @@ fn async_methods() { ); test_delete_response(&server, "http://localhost/foo", REMOVE_ALL_RESPONSE); test_delete_response(&server, "http://localhost/foo/1", REMOVE_RESPONSE); + test_get_response(&server, "http://localhost/foo/state_test", STATE_TEST_RESPONSE); }