1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-08-21 10:25:38 +02:00

make service Request type generic

This commit is contained in:
Nikolay Kim
2019-03-05 07:35:26 -08:00
parent e8a49801eb
commit dfbb77f98d
34 changed files with 622 additions and 747 deletions

View File

@@ -36,10 +36,9 @@ pub use self::then::{Then, ThenNewService};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
/// Requests handled by the service.
type Request;
///
/// `Request` - requests handled by the service.
pub trait Service<Request> {
/// Responses given by the service.
type Response;
@@ -69,22 +68,26 @@ pub trait Service {
///
/// Calling `call` without calling `poll_ready` is permitted. The
/// implementation must be resilient to this fact.
fn call(&mut self, req: Self::Request) -> Self::Future;
fn call(&mut self, req: Request) -> Self::Future;
}
/// An extension trait for `Service`s that provides a variety of convenient
/// adapters
pub trait ServiceExt: Service {
pub trait ServiceExt<Request>: Service<Request> {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
fn apply_fn<F, B, B1, Out, Req>(
self,
service: B1,
f: F,
) -> AndThenApply<Self, B, F, Out, Request, Req>
where
Self: Sized,
F: FnMut(Self::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
B: Service<Error = Self::Error>,
B1: IntoService<B>,
B: Service<Req, Error = Self::Error>,
B1: IntoService<B, Req>,
{
AndThenApply::new(self, service, f)
}
@@ -101,8 +104,8 @@ pub trait ServiceExt: Service {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where
Self: Sized,
F: IntoService<B>,
B: Service<Request = Self::Response, Error = Self::Error>,
F: IntoService<B, Self::Response>,
B: Service<Self::Response, Error = Self::Error>,
{
AndThen::new(self, service.into_service())
}
@@ -128,7 +131,7 @@ pub trait ServiceExt: Service {
fn then<B>(self, service: B) -> Then<Self, B>
where
Self: Sized,
B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>,
B: Service<Result<Self::Response, Self::Error>, Error = Self::Error>,
{
Then::new(self, service)
}
@@ -167,7 +170,7 @@ pub trait ServiceExt: Service {
}
}
impl<T: ?Sized> ServiceExt for T where T: Service {}
impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {}
/// Creates new `Service` values.
///
@@ -177,11 +180,9 @@ impl<T: ?Sized> ServiceExt for T where T: Service {}
/// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream.
///
/// `Config` is a service factory configuration type.
pub trait NewService<Config = ()> {
/// Requests handled by the service.
type Request;
/// * `Request` - requests handled by the service.
/// * `Config` - is a service factory configuration type.
pub trait NewService<Request, Config = ()> {
/// Responses given by the service
type Response;
@@ -189,11 +190,7 @@ pub trait NewService<Config = ()> {
type Error;
/// The `Service` value created by this factory
type Service: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type InitError;
@@ -206,33 +203,33 @@ pub trait NewService<Config = ()> {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1>(
fn apply<T, T1, B, B1, Req>(
self,
transform: T1,
service: B1,
) -> AndThenTransform<T, Self, B, Config>
) -> AndThenTransform<T, Self, B, Req>
where
Self: Sized,
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T: Transform<Self::Response, B::Service, InitError = Self::InitError>,
T::Error: From<Self::Error>,
T1: IntoTransform<T, B::Service>,
B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
T1: IntoTransform<T, Self::Response, B::Service>,
B: NewService<Req, Config, InitError = Self::InitError>,
B1: IntoNewService<B, Req, Config>,
{
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
}
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<B, I, F, Out>(
fn apply_fn<B, I, F, Out, Req>(
self,
service: I,
f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Config>
) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config>
where
Self: Sized,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>,
B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Req, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
@@ -241,16 +238,11 @@ pub trait NewService<Config = ()> {
}
/// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where
Self: Sized,
F: IntoNewService<B, Config>,
B: NewService<
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
F: IntoNewService<B, Self::Response, Config>,
B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>,
{
AndThenNewService::new(self, new_service)
}
@@ -278,15 +270,15 @@ pub trait NewService<Config = ()> {
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config>
where
Self: Sized,
F: IntoNewService<B, Config>,
F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>,
B: NewService<
Result<Self::Response, Self::Error>,
Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error,
InitError = Self::InitError,
>,
{
ThenNewService::new(self, new_service)
ThenNewService::new(self, new_service.into_new_service())
}
/// Map this service's output to a different type, returning a new service
@@ -318,11 +310,10 @@ pub trait NewService<Config = ()> {
}
}
impl<'a, S> Service for &'a mut S
impl<'a, S, R> Service<R> for &'a mut S
where
S: Service + 'a,
S: Service<R> + 'a,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@@ -331,16 +322,15 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: R) -> S::Future {
(**self).call(request)
}
}
impl<S> Service for Box<S>
impl<S, R> Service<R> for Box<S>
where
S: Service + ?Sized,
S: Service<R> + ?Sized,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@@ -349,16 +339,15 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: R) -> S::Future {
(**self).call(request)
}
}
impl<S, C> NewService<C> for Rc<S>
impl<S, R, C> NewService<R, C> for Rc<S>
where
S: NewService<C>,
S: NewService<R, C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@@ -370,11 +359,10 @@ where
}
}
impl<S, C> NewService<C> for Arc<S>
impl<S, R, C> NewService<R, C> for Arc<S>
where
S: NewService<C>,
S: NewService<R, C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@@ -387,35 +375,35 @@ where
}
/// Trait for types that can be converted to a `Service`
pub trait IntoService<T>
pub trait IntoService<T, R>
where
T: Service,
T: Service<R>,
{
/// Convert to a `Service`
fn into_service(self) -> T;
}
/// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, C = ()>
pub trait IntoNewService<T, R, C = ()>
where
T: NewService<C>,
T: NewService<R, C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T> IntoService<T> for T
impl<T, R> IntoService<T, R> for T
where
T: Service,
T: Service<R>,
{
fn into_service(self) -> T {
self
}
}
impl<T, C> IntoNewService<T, C> for T
impl<T, R, C> IntoNewService<T, R, C> for T
where
T: NewService<C>,
T: NewService<R, C>,
{
fn into_new_service(self) -> T {
self
@@ -423,17 +411,17 @@ where
}
/// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, C>
pub trait IntoConfigurableNewService<T, R, C>
where
T: NewService<C>,
T: NewService<R, C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T, C> IntoConfigurableNewService<T, C> for T
impl<T, R, C> IntoConfigurableNewService<T, R, C> for T
where
T: NewService<C>,
T: NewService<R, C>,
{
fn into_new_service(self) -> T {
self