-
Notifications
You must be signed in to change notification settings - Fork 321
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
@@ -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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha, nice catch