pub trait ServiceFactory<Req, Cfg = ()> {
    type Response;
    type Error;
    type Service: Service<Req, Response = Self::Response, Error = Self::Error>;
    type InitError;

    // Required method
    fn create(
        &self,
        cfg: Cfg
    ) -> impl Future<Output = Result<Self::Service, Self::InitError>>;

    // Provided methods
    async fn pipeline(
        &self,
        cfg: Cfg
    ) -> Result<Pipeline<Self::Service>, Self::InitError>
       where Self: Sized { ... }
    fn map<F, Res>(
        self,
        f: F
    ) -> ServiceChainFactory<MapFactory<Self, F, Req, Res, Cfg>, Req, Cfg>
       where Self: Sized,
             F: Fn(Self::Response) -> Res + Clone { ... }
    fn map_err<F, E>(
        self,
        f: F
    ) -> ServiceChainFactory<MapErrFactory<Self, Req, Cfg, F, E>, Req, Cfg>
       where Self: Sized,
             F: Fn(Self::Error) -> E + Clone { ... }
    fn map_init_err<F, E>(
        self,
        f: F
    ) -> ServiceChainFactory<MapInitErr<Self, Req, Cfg, F, E>, Req, Cfg>
       where Self: Sized,
             F: Fn(Self::InitError) -> E + Clone { ... }
}
Expand description

Factory for creating Services.

This is useful for cases where new Services must be produced. One case is a TCP server listener: a listener accepts new connections, constructs a new Service for each using the ServiceFactory trait, and uses the new Service to process inbound requests on that new connection.

Config is a service factory configuration type.

Simple factories may be able to use fn_factory or fn_factory_with_config to reduce boilerplate.

Required Associated Types§

source

type Response

Responses given by the created services.

source

type Error

Errors produced by the created services.

source

type Service: Service<Req, Response = Self::Response, Error = Self::Error>

The kind of Service created by this factory.

source

type InitError

Errors potentially raised while building a service.

Required Methods§

source

fn create( &self, cfg: Cfg ) -> impl Future<Output = Result<Self::Service, Self::InitError>>

Create and return a new service value asynchronously.

Provided Methods§

source

async fn pipeline( &self, cfg: Cfg ) -> Result<Pipeline<Self::Service>, Self::InitError>
where Self: Sized,

Create and return a new service value asynchronously and wrap into a container

source

fn map<F, Res>( self, f: F ) -> ServiceChainFactory<MapFactory<Self, F, Req, Res, Cfg>, Req, Cfg>
where Self: Sized, F: Fn(Self::Response) -> Res + Clone,

Map this service’s output to a different type, returning a new service of the resulting type.

source

fn map_err<F, E>( self, f: F ) -> ServiceChainFactory<MapErrFactory<Self, Req, Cfg, F, E>, Req, Cfg>
where Self: Sized, F: Fn(Self::Error) -> E + Clone,

Map this service’s error to a different error, returning a new service.

source

fn map_init_err<F, E>( self, f: F ) -> ServiceChainFactory<MapInitErr<Self, Req, Cfg, F, E>, Req, Cfg>
where Self: Sized, F: Fn(Self::InitError) -> E + Clone,

Map this factory’s init error to a different error, returning a new service.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<S, Req, Cfg> ServiceFactory<Req, Cfg> for Rc<S>
where S: ServiceFactory<Req, Cfg>,

§

type Response = <S as ServiceFactory<Req, Cfg>>::Response

§

type Error = <S as ServiceFactory<Req, Cfg>>::Error

§

type Service = <S as ServiceFactory<Req, Cfg>>::Service

§

type InitError = <S as ServiceFactory<Req, Cfg>>::InitError

source§

async fn create(&self, cfg: Cfg) -> Result<Self::Service, Self::InitError>

Implementors§

source§

impl<A, B, R, C> ServiceFactory<R, C> for ThenFactory<A, B>
where A: ServiceFactory<R, C>, B: ServiceFactory<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, C: Clone,

§

type Response = <B as ServiceFactory<Result<<A as ServiceFactory<R, C>>::Response, <A as ServiceFactory<R, C>>::Error>, C>>::Response

§

type Error = <A as ServiceFactory<R, C>>::Error

§

type Service = Then<<A as ServiceFactory<R, C>>::Service, <B as ServiceFactory<Result<<A as ServiceFactory<R, C>>::Response, <A as ServiceFactory<R, C>>::Error>, C>>::Service>

§

type InitError = <A as ServiceFactory<R, C>>::InitError

source§

impl<A, B, Req, Cfg> ServiceFactory<Req, Cfg> for AndThenFactory<A, B>
where A: ServiceFactory<Req, Cfg>, B: ServiceFactory<A::Response, Cfg, Error = A::Error, InitError = A::InitError>, Cfg: Clone,

§

type Response = <B as ServiceFactory<<A as ServiceFactory<Req, Cfg>>::Response, Cfg>>::Response

§

type Error = <A as ServiceFactory<Req, Cfg>>::Error

§

type Service = AndThen<<A as ServiceFactory<Req, Cfg>>::Service, <B as ServiceFactory<<A as ServiceFactory<Req, Cfg>>::Response, Cfg>>::Service>

§

type InitError = <A as ServiceFactory<Req, Cfg>>::InitError

source§

impl<A, F, R, C, C2> ServiceFactory<R, C> for MapConfig<A, F, C, C2>
where A: ServiceFactory<R, C2>, F: Fn(C) -> C2,

§

type Response = <A as ServiceFactory<R, C2>>::Response

§

type Error = <A as ServiceFactory<R, C2>>::Error

§

type Service = <A as ServiceFactory<R, C2>>::Service

§

type InitError = <A as ServiceFactory<R, C2>>::InitError

source§

impl<A, F, Req, Res, Cfg> ServiceFactory<Req, Cfg> for MapFactory<A, F, Req, Res, Cfg>
where A: ServiceFactory<Req, Cfg>, F: Fn(A::Response) -> Res + Clone,

§

type Response = Res

§

type Error = <A as ServiceFactory<Req, Cfg>>::Error

§

type Service = Map<<A as ServiceFactory<Req, Cfg>>::Service, F, Req, Res>

§

type InitError = <A as ServiceFactory<Req, Cfg>>::InitError

source§

impl<A, R, C> ServiceFactory<R, C> for UnitConfig<A>
where A: ServiceFactory<R>,

source§

impl<A, R, C, F, E> ServiceFactory<R, C> for MapErrFactory<A, R, C, F, E>
where A: ServiceFactory<R, C>, F: Fn(A::Error) -> E + Clone,

§

type Response = <A as ServiceFactory<R, C>>::Response

§

type Error = E

§

type Service = MapErr<<A as ServiceFactory<R, C>>::Service, F, E>

§

type InitError = <A as ServiceFactory<R, C>>::InitError

source§

impl<A, R, C, F, E> ServiceFactory<R, C> for MapInitErr<A, R, C, F, E>
where A: ServiceFactory<R, C>, F: Fn(A::InitError) -> E + Clone,

§

type Response = <A as ServiceFactory<R, C>>::Response

§

type Error = <A as ServiceFactory<R, C>>::Error

§

type Service = <A as ServiceFactory<R, C>>::Service

§

type InitError = E

source§

impl<C, Req, Res, Err, InitErr> ServiceFactory<Req, C> for BoxServiceFactory<C, Req, Res, Err, InitErr>
where Req: 'static,

§

type Response = Res

§

type Error = Err

§

type Service = BoxService<Req, Res, Err>

§

type InitError = InitErr

source§

impl<F, Fut, Cfg, Srv, Req, Err> ServiceFactory<Req, Cfg> for FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
where F: Fn(Cfg) -> Fut, Fut: Future<Output = Result<Srv, Err>>, Srv: Service<Req>,

§

type Response = <Srv as Service<Req>>::Response

§

type Error = <Srv as Service<Req>>::Error

§

type Service = Srv

§

type InitError = Err

source§

impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory<Req, Cfg> for FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
where F: Fn(Req) -> Fut + Clone, Fut: Future<Output = Result<Res, Err>>,

§

type Response = Res

§

type Error = Err

§

type Service = FnService<F, Req>

§

type InitError = ()

source§

impl<F, S, R, Req, E, C> ServiceFactory<Req, C> for FnServiceNoConfig<F, S, R, Req, E>
where F: Fn() -> R, R: Future<Output = Result<S, E>>, S: Service<Req>, C: 'static,

§

type Response = <S as Service<Req>>::Response

§

type Error = <S as Service<Req>>::Error

§

type Service = S

§

type InitError = E

source§

impl<T, Req, Cfg, F, R, In, Out, Err> ServiceFactory<In, Cfg> for ApplyFactory<T, Req, Cfg, F, R, In, Out, Err>
where T: ServiceFactory<Req, Cfg>, F: Fn(In, Pipeline<T::Service>) -> R + Clone, R: Future<Output = Result<Out, Err>>, Err: From<T::Error>,

§

type Response = Out

§

type Error = Err

§

type Service = Apply<<T as ServiceFactory<Req, Cfg>>::Service, Req, F, R, In, Out, Err>

§

type InitError = <T as ServiceFactory<Req, Cfg>>::InitError

source§

impl<T, S, R, C> ServiceFactory<R, C> for ApplyMiddleware<T, S, C>
where S: ServiceFactory<R, C>, T: Middleware<S::Service>, T::Service: Service<R>,

§

type Response = <<T as Middleware<<S as ServiceFactory<R, C>>::Service>>::Service as Service<R>>::Response

§

type Error = <<T as Middleware<<S as ServiceFactory<R, C>>::Service>>::Service as Service<R>>::Error

§

type Service = <T as Middleware<<S as ServiceFactory<R, C>>::Service>>::Service

§

type InitError = <S as ServiceFactory<R, C>>::InitError

source§

impl<T: ServiceFactory<R, C>, R, C> ServiceFactory<R, C> for ServiceChainFactory<T, R, C>

§

type Response = <T as ServiceFactory<R, C>>::Response

§

type Error = <T as ServiceFactory<R, C>>::Error

§

type Service = <T as ServiceFactory<R, C>>::Service

§

type InitError = <T as ServiceFactory<R, C>>::InitError