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

Rename factory to handler #1852

Merged
merged 8 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
## Unreleased - 2020-xx-xx
### Changed
* Bumped `rand` to `0.8`
* Rename `Handler` to `HandlerService` and rename `Factory` to `Handler`. [#1852]

### Fixed
* added the actual parsing error to `test::read_body_json` [#1812]

[#1812]: https://github.com/actix/actix-web/pull/1812


[#1852]: https://github.com/actix/actix-web/pull/1852

## 3.3.2 - 2020-12-01
### Fixed
Expand Down
93 changes: 49 additions & 44 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ use crate::request::HttpRequest;
use crate::responder::Responder;
use crate::service::{ServiceRequest, ServiceResponse};

/// Async handler converter factory
pub trait Factory<T, R, O>: Clone + 'static
/// A request handler is an async function that accepts zero or more parameters that can be
/// extracted from a request (ie, [`impl FromRequest`](crate::FromRequest)) and returns a type that can be converted into
/// an [`HttpResponse`](crate::HttpResponse) (ie, [`impl Responder`](crate::Responder)).
///
/// 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.
pub trait Handler<T, R>: Clone + 'static
where
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
fn call(&self, param: T) -> R;
}

impl<F, R, O> Factory<(), R, O> for F
impl<F, R> Handler<(), R> for F
where
F: Fn() -> R + Clone + 'static,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
fn call(&self, _: ()) -> R {
(self)()
Expand All @@ -36,53 +41,53 @@ where

#[doc(hidden)]
/// Extract arguments from request, run factory function and make response.
pub struct Handler<F, T, R, O>
pub struct HandlerService<F, T, R>
where
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
hnd: F,
_t: PhantomData<(T, R, O)>,
_t: PhantomData<(T, R)>,
}

impl<F, T, R, O> Handler<F, T, R, O>
impl<F, T, R> HandlerService<F, T, R>
where
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
pub fn new(hnd: F) -> Self {
Handler {
Self {
hnd,
_t: PhantomData,
}
}
}

impl<F, T, R, O> Clone for Handler<F, T, R, O>
impl<F, T, R> Clone for HandlerService<F, T, R>
where
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
fn clone(&self) -> Self {
Handler {
Self {
hnd: self.hnd.clone(),
_t: PhantomData,
}
}
}

impl<F, T, R, O> ServiceFactory for Handler<F, T, R, O>
impl<F, T, R> ServiceFactory for HandlerService<F, T, R>
where
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
type Request = ServiceRequest;
type Response = ServiceResponse;
Expand All @@ -97,18 +102,18 @@ where
}
}

// Handler is both it's ServiceFactory and Service Type.
impl<F, T, R, O> Service for Handler<F, T, R, O>
// Handler is both it's ServiceHandler and Service Type.
impl<F, T, R> Service for HandlerService<F, T, R>
where
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
type Request = ServiceRequest;
type Response = ServiceResponse;
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>> {
Poll::Ready(Ok(()))
Expand All @@ -123,24 +128,24 @@ where

#[doc(hidden)]
#[pin_project(project = HandlerProj)]
pub enum HandlerServiceFuture<F, T, R, O>
pub enum HandlerServiceFuture<F, T, R>
where
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
Extract(#[pin] T::Future, Option<HttpRequest>, F),
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
F: Factory<T, R, O>,
F: Handler<T, R>,
T: FromRequest,
R: Future<Output = O>,
O: Responder,
R: Future,
R::Output: Responder,
{
// Error type in this future is a placeholder type.
// all instances of error must be converted to ServiceResponse and return in Ok.
Expand Down Expand Up @@ -181,10 +186,10 @@ where

/// FromRequest trait impl for tuples
macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => {
impl<Func, $($T,)+ Res, O> Factory<($($T,)+), Res, O> for Func
impl<Func, $($T,)+ Res> Handler<($($T,)+), Res> for Func
where Func: Fn($($T,)+) -> Res + Clone + 'static,
Res: Future<Output = O>,
O: Responder,
Res: Future,
Res::Output: Responder,
{
fn call(&self, param: ($($T,)+)) -> Res {
(self)($(param.$n,)+)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub mod dev {

pub use crate::config::{AppConfig, AppService};
#[doc(hidden)]
pub use crate::handler::Factory;
pub use crate::handler::Handler;
pub use crate::info::ConnectionInfo;
pub use crate::rmap::ResourceMap;
pub use crate::service::{
Expand Down
10 changes: 5 additions & 5 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::data::Data;
use crate::dev::{insert_slash, AppService, HttpServiceFactory, ResourceDef};
use crate::extract::FromRequest;
use crate::guard::Guard;
use crate::handler::Factory;
use crate::handler::Handler;
use crate::responder::Responder;
use crate::route::{CreateRouteService, Route, RouteService};
use crate::service::{ServiceRequest, ServiceResponse};
Expand Down Expand Up @@ -227,12 +227,12 @@ where
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
/// 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
F: Factory<I, R, U>,
F: Handler<I, R>,
I: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
R: Future + 'static,
R::Output: Responder + 'static,
{
self.routes.push(Route::new().to(handler));
self
Expand Down
14 changes: 7 additions & 7 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures_util::future::{ready, FutureExt, LocalBoxFuture};

use crate::extract::FromRequest;
use crate::guard::{self, Guard};
use crate::handler::{Factory, Handler};
use crate::handler::{Handler, HandlerService};
use crate::responder::Responder;
use crate::service::{ServiceRequest, ServiceResponse};
use crate::HttpResponse;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Route {
#[allow(clippy::new_without_default)]
pub fn new() -> Route {
Route {
service: Box::new(RouteNewService::new(Handler::new(|| {
service: Box::new(RouteNewService::new(HandlerService::new(|| {
ready(HttpResponse::NotFound())
}))),
guards: Rc::new(Vec::new()),
Expand Down Expand Up @@ -219,14 +219,14 @@ 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
F: Factory<T, R, U>,
F: Handler<T, R>,
T: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
R: Future + 'static,
R::Output: Responder + 'static,
{
self.service = Box::new(RouteNewService::new(Handler::new(handler)));
self.service = Box::new(RouteNewService::new(HandlerService::new(handler)));
self
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use futures_channel::oneshot::Canceled;

use crate::error::BlockingError;
use crate::extract::FromRequest;
use crate::handler::Factory;
use crate::handler::Handler;
use crate::resource::Resource;
use crate::responder::Responder;
use crate::route::Route;
Expand Down Expand Up @@ -244,12 +244,12 @@ pub fn method(method: Method) -> Route {
/// web::to(index))
/// );
/// ```
pub fn to<F, I, R, U>(handler: F) -> Route
pub fn to<F, I, R>(handler: F) -> Route
where
F: Factory<I, R, U>,
F: Handler<I, R>,
I: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
R: Future + 'static,
R::Output: Responder + 'static,
{
Route::new().to(handler)
}
Expand Down