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

remove free lifetime in handler service #119

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Changes from all 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
28 changes: 14 additions & 14 deletions http/src/util/service/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ use std::{
use pin_project_lite::pin_project;
use xitca_service::{Service, ServiceFactory};

pub fn handler_service<'t, F, Req, T, O, Res, Err>(func: F) -> HandlerService<'t, F, T, O, Res, Err>
pub fn handler_service<F, Req, T, O, Res, Err>(func: F) -> HandlerService<F, T, O, Res, Err>
where
F: for<'a> Handler<'t, 'a, Req, T, Response = O, Error = Err>,
F: for<'a> Handler<'a, Req, T, Response = O, Error = Err>,
O: for<'a> Responder<'a, Req, Output = Res>,
{
HandlerService::new(func)
}

pub struct HandlerService<'t, F, T, O, Res, Err> {
pub struct HandlerService<F, T, O, Res, Err> {
func: F,
_p: PhantomData<(&'t (), T, O, Res, Err)>,
_p: PhantomData<(T, O, Res, Err)>,
}

impl<'t, F, T, O, Res, Err> HandlerService<'t, F, T, O, Res, Err> {
impl<F, T, O, Res, Err> HandlerService<F, T, O, Res, Err> {
pub fn new<Req>(func: F) -> Self
where
F: for<'a> Handler<'t, 'a, Req, T, Response = O, Error = Err>,
F: for<'a> Handler<'a, Req, T, Response = O, Error = Err>,
O: for<'a> Responder<'a, Req, Output = Res>,
{
Self { func, _p: PhantomData }
}
}

impl<F, T, O, Res, Err> Clone for HandlerService<'_, F, T, O, Res, Err>
impl<F, T, O, Res, Err> Clone for HandlerService<F, T, O, Res, Err>
where
F: Clone,
{
Expand All @@ -45,9 +45,9 @@ where
}
}

impl<'t, F, Req, T, O, Res, Err> ServiceFactory<Req> for HandlerService<'t, F, T, O, Res, Err>
impl<F, Req, T, O, Res, Err> ServiceFactory<Req> for HandlerService<F, T, O, Res, Err>
where
F: for<'a> Handler<'t, 'a, Req, T, Response = O, Error = Err>,
F: for<'a> Handler<'a, Req, T, Response = O, Error = Err>,
O: for<'a> Responder<'a, Req, Output = Res>,
{
type Response = Res;
Expand All @@ -63,9 +63,9 @@ where
}
}

impl<'t, F, Req, T, O, Res, Err> Service<Req> for HandlerService<'t, F, T, O, Res, Err>
impl<F, Req, T, O, Res, Err> Service<Req> for HandlerService<F, T, O, Res, Err>
where
F: for<'a> Handler<'t, 'a, Req, T, Response = O, Error = Err>,
F: for<'a> Handler<'a, Req, T, Response = O, Error = Err>,
O: for<'a> Responder<'a, Req, Output = Res>,
{
type Response = Res;
Expand Down Expand Up @@ -101,17 +101,17 @@ where
/// `F: for<'a> FnArgs<<T as FromRequest>::Type<'a>>,`
/// `for<'a> FnArgs<T::Type<'a>>::Output: Future`
/// But these are known to be buggy. See https://github.com/rust-lang/rust/issues/56556
pub trait Handler<'t, 'a, Req, T>: Clone {
pub trait Handler<'a, Req, T>: Clone {
type Error;
type Response;
type Future: Future<Output = Result<Self::Response, Self::Error>>;

fn handle(&'a self, req: &'a mut Req) -> Self::Future;
}

impl<'t, 'a, Req, T, Res, Err, F> Handler<'t, 'a, Req, T> for F
impl<'a, Req, T, Res, Err, F> Handler<'a, Req, T> for F
where
T: FromRequest<'t, Req, Error = Err>,
T: FromRequest<'static, Req, Error = Err>,
F: AsyncFn<T::Type<'a>, Output = Res> + Clone,
F: AsyncFn<T>, // second bound to assist type inference to pinpoint T
{
Expand Down