simplify handler trait

This commit is contained in:
ibraheemdev 2020-12-25 15:57:29 -05:00
parent bacc5f5e3e
commit 30643d5648
4 changed files with 52 additions and 52 deletions

View File

@ -20,19 +20,19 @@ use crate::service::{ServiceRequest, ServiceResponse};
/// ///
/// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not /// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not
/// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information. /// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information.
pub trait Handler<T, R, O>: Clone + 'static pub trait Handler<T, R>: Clone + 'static
where where
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
fn call(&self, param: T) -> R; fn call(&self, param: T) -> R;
} }
impl<F, R, O> Handler<(), R, O> for F impl<F, R> Handler<(), R> for F
where where
F: Fn() -> R + Clone + 'static, F: Fn() -> R + Clone + 'static,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
fn call(&self, _: ()) -> R { fn call(&self, _: ()) -> R {
(self)() (self)()
@ -41,23 +41,23 @@ where
#[doc(hidden)] #[doc(hidden)]
/// Extract arguments from request, run factory function and make response. /// Extract arguments from request, run factory function and make response.
pub struct HandlerService<F, T, R, O> pub struct HandlerService<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
hnd: F, hnd: F,
_t: PhantomData<(T, R, O)>, _t: PhantomData<(T, R)>,
} }
impl<F, T, R, O> HandlerService<F, T, R, O> impl<F, T, R> HandlerService<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
pub fn new(hnd: F) -> Self { pub fn new(hnd: F) -> Self {
Self { Self {
@ -67,12 +67,12 @@ where
} }
} }
impl<F, T, R, O> Clone for HandlerService<F, T, R, O> impl<F, T, R> Clone for HandlerService<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -82,12 +82,12 @@ where
} }
} }
impl<F, T, R, O> ServiceFactory for HandlerService<F, T, R, O> impl<F, T, R> ServiceFactory for HandlerService<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
type Request = ServiceRequest; type Request = ServiceRequest;
type Response = ServiceResponse; type Response = ServiceResponse;
@ -103,17 +103,17 @@ where
} }
// Handler is both it's ServiceHandler and Service Type. // Handler is both it's ServiceHandler and Service Type.
impl<F, T, R, O> Service for HandlerService<F, T, R, O> impl<F, T, R> Service for HandlerService<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
type Request = ServiceRequest; type Request = ServiceRequest;
type Response = ServiceResponse; type Response = ServiceResponse;
type Error = Error; type Error = Error;
type Future = HandlerServiceFuture<F, T, R, O>; type Future = HandlerServiceFuture<F, T, R>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
@ -128,24 +128,24 @@ where
#[doc(hidden)] #[doc(hidden)]
#[pin_project(project = HandlerProj)] #[pin_project(project = HandlerProj)]
pub enum HandlerServiceFuture<F, T, R, O> pub enum HandlerServiceFuture<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
Extract(#[pin] T::Future, Option<HttpRequest>, F), Extract(#[pin] T::Future, Option<HttpRequest>, F),
Handle(#[pin] R, Option<HttpRequest>), Handle(#[pin] R, Option<HttpRequest>),
Respond(#[pin] O::Future, Option<HttpRequest>), Respond(#[pin] <R::Output as Responder>::Future, Option<HttpRequest>),
} }
impl<F, T, R, O> Future for HandlerServiceFuture<F, T, R, O> impl<F, T, R> Future for HandlerServiceFuture<F, T, R>
where where
F: Handler<T, R, O>, F: Handler<T, R>,
T: FromRequest, T: FromRequest,
R: Future<Output = O>, R: Future,
O: Responder, R::Output: Responder,
{ {
// Error type in this future is a placeholder type. // Error type in this future is a placeholder type.
// all instances of error must be converted to ServiceResponse and return in Ok. // all instances of error must be converted to ServiceResponse and return in Ok.
@ -186,10 +186,10 @@ where
/// FromRequest trait impl for tuples /// FromRequest trait impl for tuples
macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => { macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => {
impl<Func, $($T,)+ Res, O> Handler<($($T,)+), Res, O> for Func impl<Func, $($T,)+ Res> Handler<($($T,)+), Res> for Func
where Func: Fn($($T,)+) -> Res + Clone + 'static, where Func: Fn($($T,)+) -> Res + Clone + 'static,
Res: Future<Output = O>, Res: Future,
O: Responder, Res::Output: Responder,
{ {
fn call(&self, param: ($($T,)+)) -> Res { fn call(&self, param: ($($T,)+)) -> Res {
(self)($(param.$n,)+) (self)($(param.$n,)+)

View File

@ -227,12 +227,12 @@ where
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() } /// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
/// App::new().service(web::resource("/").route(web::route().to(index))); /// App::new().service(web::resource("/").route(web::route().to(index)));
/// ``` /// ```
pub fn to<F, I, R, U>(mut self, handler: F) -> Self pub fn to<F, I, R>(mut self, handler: F) -> Self
where where
F: Handler<I, R, U>, F: Handler<I, R>,
I: FromRequest + 'static, I: FromRequest + 'static,
R: Future<Output = U> + 'static, R: Future + 'static,
U: Responder + 'static, R::Output: Responder + 'static,
{ {
self.routes.push(Route::new().to(handler)); self.routes.push(Route::new().to(handler));
self self

View File

@ -219,12 +219,12 @@ impl Route {
/// ); /// );
/// } /// }
/// ``` /// ```
pub fn to<F, T, R, U>(mut self, handler: F) -> Self pub fn to<F, T, R>(mut self, handler: F) -> Self
where where
F: Handler<T, R, U>, F: Handler<T, R>,
T: FromRequest + 'static, T: FromRequest + 'static,
R: Future<Output = U> + 'static, R: Future + 'static,
U: Responder + 'static, R::Output: Responder + 'static,
{ {
self.service = Box::new(RouteNewService::new(HandlerService::new(handler))); self.service = Box::new(RouteNewService::new(HandlerService::new(handler)));
self self

View File

@ -244,12 +244,12 @@ pub fn method(method: Method) -> Route {
/// web::to(index)) /// web::to(index))
/// ); /// );
/// ``` /// ```
pub fn to<F, I, R, U>(handler: F) -> Route pub fn to<F, I, R>(handler: F) -> Route
where where
F: Handler<I, R, U>, F: Handler<I, R>,
I: FromRequest + 'static, I: FromRequest + 'static,
R: Future<Output = U> + 'static, R: Future + 'static,
U: Responder + 'static, R::Output: Responder + 'static,
{ {
Route::new().to(handler) Route::new().to(handler)
} }