Skip to main content

ServiceFactory

Trait ServiceFactory 

Source
pub trait ServiceFactory<St, Req> {
    type Res;
    type Error;
    type Service: Service<St, Req, Res = Self::Res, Error = Self::Error>;
    type InitError;

    // Required method
    async fn create(&self, cfg: &St) -> Result<Self::Service, Self::InitError>;

    // Provided methods
    async fn pipeline(
        &self,
        st: St,
    ) -> Result<Pipeline<Req, Self::Res, Self::Error>, Self::InitError>
       where Self: 'static,
             St: 'static,
             Req: 'static { ... }
    fn map<F, Res>(
        self,
        f: F,
    ) -> ServiceChainFactory<MapFactory<F, Self, Res>, St, Req>
       where Self: Sized,
             F: Fn(Self::Res) -> Res + Clone { ... }
    fn map_err<F, E>(
        self,
        f: F,
    ) -> ServiceChainFactory<MapErrFactory<F, Self, E>, St, Req>
       where Self: Sized,
             F: Fn(Self::Error) -> E + Clone { ... }
    fn map_init_err<F, E>(
        self,
        f: F,
    ) -> ServiceChainFactory<MapInitErr<F, Self, E>, St, Req>
       where Self: Sized,
             F: Fn(Self::InitError) -> E + Clone { ... }
    fn and_then<U, F>(
        self,
        f: F,
    ) -> ServiceChainFactory<AndThenFactory<Self, U>, St, Req>
       where Self: Sized,
             U: ServiceFactory<St, Self::Res, Error = Self::Error, InitError = Self::InitError>,
             F: IntoServiceFactory<U, St, Self::Res> { ... }
    fn boxed(
        self,
    ) -> BoxServiceFactory<St, Req, Self::Res, Self::Error, Self::InitError>
       where St: 'static,
             Req: 'static,
             Self: Sized + 'static { ... }
}
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 Res

Responses given by the created services.

Source

type Error

Errors produced by the created services.

Source

type Service: Service<St, Req, Res = Self::Res, 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: &St) -> Result<Self::Service, Self::InitError>

Creates a new service asynchronously and returns it.

Provided Methods§

Source

async fn pipeline( &self, st: St, ) -> Result<Pipeline<Req, Self::Res, Self::Error>, Self::InitError>
where Self: 'static, St: 'static, Req: 'static,

Asynchronously creates a new service and wraps it in a container.

Source

fn map<F, Res>( self, f: F, ) -> ServiceChainFactory<MapFactory<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> 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<F, Self, E>, St, Req>
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<F, Self, E>, St, Req>
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 and_then<U, F>( self, f: F, ) -> ServiceChainFactory<AndThenFactory<Self, U>, St, Req>
where Self: Sized, U: ServiceFactory<St, Self::Res, Error = Self::Error, InitError = Self::InitError>, F: IntoServiceFactory<U, St, Self::Res>,

Call another service after call to this one has resolved successfully.

Source

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

Creates a boxed service factory.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<Sf, St, Req> ServiceFactory<St, Req> for Rc<Sf>
where Sf: ServiceFactory<St, Req>,

Source§

type Res = <Sf as ServiceFactory<St, Req>>::Res

Source§

type Error = <Sf as ServiceFactory<St, Req>>::Error

Source§

type Service = <Sf as ServiceFactory<St, Req>>::Service

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

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

Implementors§

Source§

impl<A, B, St, Req> ServiceFactory<St, Req> for AndThenFactory<A, B>
where A: ServiceFactory<St, Req>, B: ServiceFactory<St, A::Res, Error = A::Error, InitError = A::InitError>,

Source§

type Res = <B as ServiceFactory<St, <A as ServiceFactory<St, Req>>::Res>>::Res

Source§

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

Source§

type Service = AndThen<<A as ServiceFactory<St, Req>>::Service, <B as ServiceFactory<St, <A as ServiceFactory<St, Req>>::Res>>::Service>

Source§

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

Source§

impl<A, B, St, Req> ServiceFactory<St, Req> for ThenFactory<A, B>
where A: ServiceFactory<St, Req>, B: ServiceFactory<St, Result<A::Res, A::Error>, Error = A::Error, InitError = A::InitError>,

Source§

type Res = <B as ServiceFactory<St, Result<<A as ServiceFactory<St, Req>>::Res, <A as ServiceFactory<St, Req>>::Error>>>::Res

Source§

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

Source§

type Service = Then<<A as ServiceFactory<St, Req>>::Service, <B as ServiceFactory<St, Result<<A as ServiceFactory<St, Req>>::Res, <A as ServiceFactory<St, Req>>::Error>>>::Service>

Source§

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

Source§

impl<F, Err, St, Req> ServiceFactory<St, Req> for FnReadiness<F, Err>
where F: AsyncFn(&St) -> Result<(), Err> + Clone,

Source§

type Res = Req

Source§

type Error = Err

Source§

type Service = FnReadiness<F, Err>

Source§

type InitError = !

Source§

impl<F, S, St, Req, Err> ServiceFactory<St, Req> for FnFactory<F, S, St, Req, Err>
where F: AsyncFn(&St) -> Result<S, Err>, S: Service<St, Req>,

Source§

type Res = <S as Service<St, Req>>::Res

Source§

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

Source§

type Service = S

Source§

type InitError = Err

Source§

impl<F, Sf, St, Req, E> ServiceFactory<St, Req> for MapErrFactory<F, Sf, E>
where Sf: ServiceFactory<St, Req>, F: Fn(Sf::Error) -> E + Clone,

Source§

type Res = <Sf as ServiceFactory<St, Req>>::Res

Source§

type Error = E

Source§

type Service = MapErr<F, <Sf as ServiceFactory<St, Req>>::Service, E>

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

impl<F, Sf, St, Req, Err> ServiceFactory<St, Req> for MapInitErr<F, Sf, Err>
where F: Fn(Sf::InitError) -> Err + Clone, Sf: ServiceFactory<St, Req>,

Source§

type Res = <Sf as ServiceFactory<St, Req>>::Res

Source§

type Error = <Sf as ServiceFactory<St, Req>>::Error

Source§

type Service = <Sf as ServiceFactory<St, Req>>::Service

Source§

type InitError = Err

Source§

impl<F, Sf, St, Req, In, Out, Err> ServiceFactory<St, In> for ApplyFactory<F, Sf, St, Req, In, Out, Err>
where F: AsyncFn(In, &ApplyCtx<'_, Sf::Service, St, Req>) -> Result<Out, Err> + Clone, Sf: ServiceFactory<St, Req>, Err: From<Sf::Error>,

Source§

type Res = Out

Source§

type Error = Err

Source§

type Service = Apply<<Sf as ServiceFactory<St, Req>>::Service, St, Req, F, In, Out, Err>

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

impl<F, Sf, St, Req, Res> ServiceFactory<St, Req> for MapFactory<F, Sf, Res>
where F: Fn(Sf::Res) -> Res + Clone, Sf: ServiceFactory<St, Req>,

Source§

type Res = Res

Source§

type Error = <Sf as ServiceFactory<St, Req>>::Error

Source§

type Service = Map<F, <Sf as ServiceFactory<St, Req>>::Service, Res>

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

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

Source§

type Res = Req

Source§

type Error = Err

Source§

type Service = FnShutdown<F, Err>

Source§

type InitError = !

Source§

impl<F, St, Req, Res, Err> ServiceFactory<St, Req> for FnServiceStFactory<F, St, Req, Res, Err>
where F: AsyncFn(&St, Req) -> Result<Res, Err> + Clone,

Source§

type Res = Res

Source§

type Error = Err

Source§

type Service = FnServiceSt<F, St, Req, Res, Err>

Source§

type InitError = !

Source§

impl<M, Sf, St, Req> ServiceFactory<St, Req> for ApplyMiddleware<M, Sf>
where Sf: ServiceFactory<St, Req>, M: Middleware<Sf::Service, St>, M::Service: Service<St, Req>,

Source§

type Res = <<M as Middleware<<Sf as ServiceFactory<St, Req>>::Service, St>>::Service as Service<St, Req>>::Res

Source§

type Error = <<M as Middleware<<Sf as ServiceFactory<St, Req>>::Service, St>>::Service as Service<St, Req>>::Error

Source§

type Service = <M as Middleware<<Sf as ServiceFactory<St, Req>>::Service, St>>::Service

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

impl<OtSt, Sf, St, Req> ServiceFactory<OtSt, Req> for MapStateFactory<Sf, St>
where Sf: ServiceFactory<St, Req>, St: Clone,

Source§

type Res = <Sf as ServiceFactory<St, Req>>::Res

Source§

type Error = <Sf as ServiceFactory<St, Req>>::Error

Source§

type Service = MapState<<Sf as ServiceFactory<St, Req>>::Service, St>

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

impl<Sf: ServiceFactory<St, Req>, St, Req> ServiceFactory<St, Req> for ServiceChainFactory<Sf, St, Req>

Source§

type Res = <Sf as ServiceFactory<St, Req>>::Res

Source§

type Error = <Sf as ServiceFactory<St, Req>>::Error

Source§

type Service = <Sf as ServiceFactory<St, Req>>::Service

Source§

type InitError = <Sf as ServiceFactory<St, Req>>::InitError

Source§

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

Source§

type Res = Res

Source§

type Error = Err

Source§

type Service = BoxService<St, Req, Res, Err>

Source§

type InitError = InitErr