Skip to content

Commit

Permalink
Relieve lifetime bound of IntoResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
tirr-c committed Jan 17, 2019
1 parent ef44067 commit fe734d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
23 changes: 8 additions & 15 deletions examples/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use http::status::StatusCode;
use juniper::graphql_object;
use std::sync::{atomic, Arc};
use tide::{body, App, AppData, Response};
use tide::{body, App, AppData, IntoResponse, Response};

// First, we define `Context` that holds accumulator state. This is accessible as App data in
// Tide, and as executor context in Juniper.
Expand Down Expand Up @@ -48,21 +48,14 @@ type Schema = juniper::RootNode<'static, Query, Mutation>;
async fn handle_graphql(
ctx: AppData<Context>,
query: body::Json<juniper::http::GraphQLRequest>,
) -> Result<Response, StatusCode> {
) -> Response {
let response = query.execute(&Schema::new(Query, Mutation), &ctx);

// `response` has the lifetime of `request`, so we can't use `IntoResponse` directly.
let body_vec = serde_json::to_vec(&response).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

http::Response::builder()
.status(if response.is_ok() {
StatusCode::OK
} else {
StatusCode::BAD_REQUEST
})
.header("Content-Type", "application/json")
.body(body::Body::from(body_vec))
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
let status = if response.is_ok() {
StatusCode::OK
} else {
StatusCode::BAD_REQUEST
};
body::Json(response).with_status(status).into_response()
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<T: Send + serde::de::DeserializeOwned + 'static, S: 'static> Extract<S> for
}
}

impl<T: 'static + Send + serde::Serialize> IntoResponse for Json<T> {
impl<T: Send + serde::Serialize> IntoResponse for Json<T> {
fn into_response(self) -> Response {
// TODO: think about how to handle errors
http::Response::builder()
Expand Down
6 changes: 3 additions & 3 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::body::Body;
pub type Response = http::Response<Body>;

/// A value that is synchronously convertable into a `Response`.
pub trait IntoResponse: Send + 'static + Sized {
pub trait IntoResponse: Send + Sized {
/// Convert the value into a `Response`.
fn into_response(self) -> Response;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl IntoResponse for String {
}
}

impl IntoResponse for &'static str {
impl IntoResponse for &'_ str {
fn into_response(self) -> Response {
self.to_string().into_response()
}
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<T: IntoResponse, U: IntoResponse> IntoResponse for Result<T, U> {
}
}

impl<T: Send + 'static + Into<Body>> IntoResponse for http::Response<T> {
impl<T: Send + Into<Body>> IntoResponse for http::Response<T> {
fn into_response(self) -> Response {
self.map(Into::into)
}
Expand Down

0 comments on commit fe734d7

Please sign in to comment.