Skip to content

Commit

Permalink
Follow up to http-rs#156
Browse files Browse the repository at this point in the history
  • Loading branch information
aturon committed Apr 10, 2019
1 parent adba649 commit a3f74da
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
5 changes: 0 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ impl<AppData> Context<AppData> {
&self.request
}

/// Mutably access the request body
pub fn body(&mut self) -> &mut http_service::Body {
self.request.body_mut()
}

/// Access app-global data.
pub fn app_data(&self) -> &AppData {
&self.app_data
Expand Down
2 changes: 1 addition & 1 deletion src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<AppData> ExtractCookies for Context<AppData> {
.unwrap_or_else(|| CookieData {
content: self
.headers()
.get("Cookie")
.get("tide-cookie")
.and_then(|raw| parse_from_header(raw.to_str().unwrap()).ok())
.unwrap_or_default(),
});
Expand Down
26 changes: 5 additions & 21 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use futures::future::{Future, FutureObj};
use std::pin::Pin;

use crate::{response::IntoResponse, Context, Response};

Expand Down Expand Up @@ -49,26 +48,11 @@ where
Fut: Future + Send + 'static,
Fut::Output: IntoResponse,
{
type Fut = ResponseWrapper<Fut>;
type Fut = FutureObj<'static, Response>;
fn call(&self, cx: Context<AppData>) -> Self::Fut {
ResponseWrapper { fut: (self)(cx) }
}
}

/// The future retured by the endpoint implementation for `Fn` types.
pub struct ResponseWrapper<F> {
fut: F,
}

impl<F> Future for ResponseWrapper<F>
where
F: Future,
F::Output: IntoResponse,
{
type Output = Response;

fn poll(self: Pin<&mut Self>, waker: &std::task::Waker) -> std::task::Poll<Response> {
let inner = unsafe { self.map_unchecked_mut(|wrapper| &mut wrapper.fut) };
inner.poll(waker).map(IntoResponse::into_response)
let fut = (self)(cx);
box_async! {
await!(fut).into_response()
}
}
}
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ pub trait ResultExt<T>: Sized {
StatusCode: HttpTryFrom<S>;
}

/// Extends the `Response` type with a method to extract error causes when applicable.
pub trait ResponseExt {
/// Extract the cause of the unsuccessful response, if any
fn err_cause(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)>;
}

impl<T> ResponseExt for Response<T> {
fn err_cause(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
self.extensions().get().map(|Cause(c)| &**c)
}
}

impl<T, E: std::error::Error + Send + Sync + 'static> ResultExt<T> for std::result::Result<T, E> {
fn with_err_status<S>(self, status: S) -> EndpointResult<T>
where
Expand Down

0 comments on commit a3f74da

Please sign in to comment.