Skip to main content

ServiceFactory

Trait ServiceFactory 

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

    // Required method
    async fn create(&self, cfg: Cfg) -> 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 { ... }
    fn boxed(
        self,
    ) -> BoxServiceFactory<Cfg, Req, Self::Response, Self::Error, Self::InitError>
       where Cfg: 'static,
             Req: 'static,
             Self: 'static + Sized { ... }
}
Expand description

A factory for creating Services.

This is useful when new Services must be produced dynamically. For example, a TCP server listener accepts new connections, constructs a new Service for each connection using the ServiceFactory trait, and uses that service to handle inbound requests.

Config represents the configuration type for the service factory.

Simple factories can often 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 type of Service produced by this factory.

Source

type InitError

Possible errors encountered during service construction.

Required Methods§

Source

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

Creates a new service asynchronously and returns it.

Provided Methods§

Source

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

Asynchronously creates a new service and wraps it in 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,

Returns a new service that maps this service’s output to a different 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,

Transforms this service’s error into another error, producing 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,

Maps this factory’s initialization error to a different error, returning a new service factory.

Source

fn boxed( self, ) -> BoxServiceFactory<Cfg, Req, Self::Response, Self::Error, Self::InitError>
where Cfg: 'static, Req: 'static, Self: 'static + Sized,

Creates a boxed service factory.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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>,

Source§

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

Source§

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

Source§

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

Source§

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,

Source§

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

Source§

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

Source§

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>

Source§

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,

Source§

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

Source§

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

Source§

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

Source§

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,

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,

Source§

type Response = Res

Source§

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

Source§

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

Source§

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,

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,

Source§

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

Source§

type Response = Res

Source§

type Error = Err

Source§

type Service = BoxService<Req, Res, Err>

Source§

type InitError = InitErr

Source§

impl<F, Cfg, Srv, Req, Err> ServiceFactory<Req, Cfg> for FnServiceConfig<F, Cfg, Srv, Req, Err>
where F: AsyncFn(Cfg) -> Result<Srv, Err>, Srv: Service<Req>,

Source§

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

Source§

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

Source§

type Service = Srv

Source§

type InitError = Err

Source§

impl<F, Req, Res, Err, Cfg> ServiceFactory<Req, Cfg> for FnServiceFactory<F, Req, Res, Err, Cfg>
where F: AsyncFn(Req) -> Result<Res, Err> + Clone,

Source§

impl<F, S, Req, E, C> ServiceFactory<Req, C> for FnServiceNoConfig<F, S, Req, E>
where F: AsyncFn() -> Result<S, E>, S: Service<Req>, C: 'static,

Source§

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

Source§

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

Source§

type Service = S

Source§

type InitError = E

Source§

impl<Fac: ServiceFactory<Req, C>, Req, C> ServiceFactory<Req, C> for ServiceChainFactory<Fac, Req, C>

Source§

type Response = <Fac as ServiceFactory<Req, C>>::Response

Source§

type Error = <Fac as ServiceFactory<Req, C>>::Error

Source§

type Service = <Fac as ServiceFactory<Req, C>>::Service

Source§

type InitError = <Fac as ServiceFactory<Req, C>>::InitError

Source§

impl<M, Fac, Req, Cfg> ServiceFactory<Req, Cfg> for ApplyMiddleware<M, Fac, Cfg>
where Fac: ServiceFactory<Req, Cfg>, M: Middleware<Fac::Service, Cfg>, M::Service: Service<Req>, Cfg: Clone,

Source§

type Response = <<M as Middleware<<Fac as ServiceFactory<Req, Cfg>>::Service, Cfg>>::Service as Service<Req>>::Response

Source§

type Error = <<M as Middleware<<Fac as ServiceFactory<Req, Cfg>>::Service, Cfg>>::Service as Service<Req>>::Error

Source§

type Service = <M as Middleware<<Fac as ServiceFactory<Req, Cfg>>::Service, Cfg>>::Service

Source§

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

Source§

impl<Req, Err, C, F> ServiceFactory<Req, C> for FnShutdown<Req, Err, F>
where F: AsyncFnOnce() + Clone,

Source§

type Response = Req

Source§

type Error = Err

Source§

type Service = FnShutdown<Req, Err, F>

Source§

type InitError = ()

Source§

impl<S, F, R, C> ServiceFactory<R, C> for InspectErrFactory<S, F>
where S: ServiceFactory<R, C>, F: Fn(&S::Error) + Clone,

Source§

impl<T, Req, Cfg, F, In, Out, Err> ServiceFactory<In, Cfg> for ApplyFactory<T, Req, Cfg, F, In, Out, Err>
where T: ServiceFactory<Req, Cfg>, F: AsyncFn(In, &Pipeline<T::Service>) -> Result<Out, Err> + Clone, Err: From<T::Error>,

Source§

type Response = Out

Source§

type Error = Err

Source§

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

Source§

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