Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Endpoint::call generic over lifetime #397

Merged
merged 3 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,19 @@ use crate::{response::IntoResponse, Request, Response};
///
/// Tide routes will also accept endpoints with `Fn` signatures of this form, but using the `async` keyword has better ergonomics.
pub trait Endpoint<State>: Send + Sync + 'static {
/// The async result of `call`.
type Fut: Future<Output = Response> + Send + 'static;

/// Invoke the endpoint within the given context
fn call(&self, req: Request<State>) -> Self::Fut;
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, Response>;
}

pub(crate) type DynEndpoint<State> =
dyn (Fn(Request<State>) -> BoxFuture<'static, Response>) + 'static + Send + Sync;
pub(crate) type DynEndpoint<State> = dyn Endpoint<State>;

impl<State, F: Send + Sync + 'static, Fut> Endpoint<State> for F
where
F: Fn(Request<State>) -> Fut,
Fut: Future + Send + 'static,
Fut::Output: IntoResponse,
{
type Fut = BoxFuture<'static, Response>;
fn call(&self, req: Request<State>) -> Self::Fut {
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, Response> {
let fut = (self)(req);
Box::pin(async move { fut.await.into_response() })
}
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a, State: 'static> Next<'a, State> {
self.next_middleware = next;
current.handle(req, self)
} else {
(self.endpoint)(req)
self.endpoint.call(req)
}
}
}
26 changes: 4 additions & 22 deletions src/redirect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use async_std::future;
use async_std::task::{Context, Poll};

use std::pin::Pin;

use crate::utils::BoxFuture;
use crate::{Endpoint, Request, Response};

/// Redirect a route to another route.
Expand All @@ -21,7 +17,7 @@ use crate::{Endpoint, Request, Response};
/// app.listen("127.0.0.1:8080").await?;
/// #
/// # Ok(()) }) }
/// ````
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, nice catch

/// ```
pub fn redirect<State>(location: impl AsRef<str>) -> impl Endpoint<State> {
let location = location.as_ref().to_owned();
Redirect { location }
Expand All @@ -33,22 +29,8 @@ pub struct Redirect {
}

impl<State> Endpoint<State> for Redirect {
type Fut = Future;

fn call(&self, _req: Request<State>) -> Self::Fut {
fn call<'a>(&'a self, _req: Request<State>) -> BoxFuture<'a, Response> {
let res = Response::new(307).set_header("Location", &self.location);
Future { res: Some(res) }
}
}

/// Future returned from `redirect`.
pub struct Future {
res: Option<Response>,
}

impl future::Future for Future {
type Output = Response;
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(self.res.take().unwrap())
Box::pin(async move { res })
}
}
5 changes: 2 additions & 3 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ impl<State: 'static> Router<State> {
self.method_map
.entry(method)
.or_insert_with(MethodRouter::new)
.add(path, Box::new(move |cx| Box::pin(ep.call(cx))))
.add(path, Box::new(ep))
}

pub(crate) fn add_all(&mut self, path: &str, ep: impl Endpoint<State>) {
self.all_method_router
.add(path, Box::new(move |cx| Box::pin(ep.call(cx))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, getting rid of this is nice!

self.all_method_router.add(path, Box::new(ep))
}

pub(crate) fn route(&self, path: &str, method: http::Method) -> Selection<'_, State> {
Expand Down
19 changes: 13 additions & 6 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,23 @@ impl<State: Send + Sync + 'static> Server<State> {
///
/// This type is useful only in conjunction with the [`HttpService`] trait,
/// i.e. for hosting a Tide app within some custom HTTP server.
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct Service<State> {
router: Arc<Router<State>>,
state: Arc<State>,
middleware: Arc<Vec<Arc<dyn Middleware<State>>>>,
}

impl<State> Clone for Service<State> {
fn clone(&self) -> Self {
Self {
router: self.router.clone(),
state: self.state.clone(),
middleware: self.middleware.clone(),
}
}
}

Comment on lines +331 to +340
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference with the Derive block? It looks like there should be no difference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derive block adds State: Clone bound, which is undesirable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I thought that bound was intentional. How else do we guarantee that the State can always be cloned for each route?

I feel like I'm missing something here 😅

#[derive(Debug)]
pub struct ReadyFuture;

Expand All @@ -351,17 +360,15 @@ impl<State: Sync + Send + 'static> HttpService for Service<State> {

fn respond(&self, _conn: &mut (), req: http_service::Request) -> Self::ResponseFuture {
let req = Request::new(self.state.clone(), req, Vec::new());
let fut = self.call(req);
Box::pin(async move { Ok(fut.await.into()) })
let service = self.clone();
Box::pin(async move { Ok(service.call(req).await.into()) })
}
}

impl<State: Sync + Send + 'static, InnerState: Sync + Send + 'static> Endpoint<State>
for Service<InnerState>
{
type Fut = BoxFuture<'static, Response>;

fn call(&self, req: Request<State>) -> Self::Fut {
fn call<'a>(&'a self, req: Request<State>) -> BoxFuture<'a, Response> {
let Request {
request: req,
mut route_params,
Expand Down
7 changes: 3 additions & 4 deletions src/server/route.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{router::Router, Endpoint};
use crate::utils::BoxFuture;
use crate::{router::Router, Endpoint, Response};

/// A handle to a route.
///
Expand Down Expand Up @@ -172,9 +173,7 @@ impl<E> Clone for StripPrefixEndpoint<E> {
}

impl<State, E: Endpoint<State>> Endpoint<State> for StripPrefixEndpoint<E> {
type Fut = E::Fut;

fn call(&self, mut req: crate::Request<State>) -> Self::Fut {
fn call<'a>(&'a self, mut req: crate::Request<State>) -> BoxFuture<'a, Response> {
let rest = req.rest().unwrap_or("");
let mut path_and_query = format!("/{}", rest);
let uri = req.uri();
Expand Down