Struct service_async::stack::FactoryStack
source · pub struct FactoryStack<C, S> { /* private fields */ }
Expand description
A powerful abstraction for creating complex service chains by managing a stack of service factories.
FactoryStack
allows for the composition of multiple FactoryLayer
s, where each layer
can add functionality, modify behavior of inner layers, or transform the service chain.
The FactoryStack
works by composing multiple FactoryLayer
s together. Each layer in the stack
wraps the layers below it, creating a nested structure of factories. When you call make
or
make_async
on a FactoryStack
, it traverses this structure from the outermost layer to the
innermost, creating the complete service chain.
This approach allows users to create complex service factories by chaining multiple factory layers together in an intuitive manner. Each layer can add its own functionality, modify the behavior of inner layers, or even completely transform the service chain.
§Usage
To create a chain of services using FactoryStack
:
- Start with a
FactoryStack
initialized with your configuration. - Use the
push
method to add layers to the stack. - Each layer can modify or enhance the behavior of the inner layers.
- Finally, call
make
ormake_async
to create the complete service chain.
This system offers a powerful and flexible way to construct and update service chains while managing state and resources efficiently. It allows for modular, reusable pieces of functionality, easy reconfiguration of service chains, and clear separation of concerns between different parts of your service logic.
§Example
use service_async::{layer::FactoryLayer, stack::FactoryStack, MakeService, Service};
struct Config { /* ... */ }
struct ServiceA;
struct ServiceB<T>(T);
impl<C> FactoryLayer<C, ()> for ServiceA {
type Factory = Self;
fn layer(&self, _: &C, _: ()) -> Self::Factory { ServiceA }
}
impl<C, T> FactoryLayer<C, T> for ServiceB<T> {
type Factory = Self;
fn layer(&self, _: &C, inner: T) -> Self::Factory { ServiceB(inner) }
}
let config = Config { /* ... */ };
let stack = FactoryStack::new(config)
.push(ServiceA::layer())
.push(ServiceB::layer());
let service = stack.make().expect("Failed to create service");
This example demonstrates how to create a FactoryStack
with two layers (ServiceA
and ServiceB
)
and then use it to create a service.
Implementations§
source§impl<C, F> FactoryStack<C, F>
impl<C, F> FactoryStack<C, F>
sourcepub fn replace<NF>(self, factory: NF) -> FactoryStack<C, NF>
pub fn replace<NF>(self, factory: NF) -> FactoryStack<C, NF>
Replace inner with a new factory.
sourcepub fn push<L>(self, layer: L) -> FactoryStack<C, L::Factory>where
L: FactoryLayer<C, F>,
pub fn push<L>(self, layer: L) -> FactoryStack<C, L::Factory>where
L: FactoryLayer<C, F>,
Push a new factory layer.
sourcepub fn into_async(self) -> FactoryStack<C, AsyncMakeServiceWrapper<F>>
pub fn into_async(self) -> FactoryStack<C, AsyncMakeServiceWrapper<F>>
Convert the factory to an async factory.
sourcepub fn push_map_target<M: Clone>(
self,
f: M,
) -> FactoryStack<C, MapTargetService<F, M>>
pub fn push_map_target<M: Clone>( self, f: M, ) -> FactoryStack<C, MapTargetService<F, M>>
Push a new factory of service to map the request type.
sourcepub fn into_boxed_service<Req>(
self,
) -> FactoryStack<C, BoxServiceFactory<F, Req>>
pub fn into_boxed_service<Req>( self, ) -> FactoryStack<C, BoxServiceFactory<F, Req>>
Convert the factory to factory of BoxedService
.
Works for MakeService and AsyncMakeService.
sourcepub fn push_boxed_service<Req>(
self,
) -> FactoryStack<C, BoxServiceFactory<F, Req>>
👎Deprecated: use into_boxed_service
instead
pub fn push_boxed_service<Req>( self, ) -> FactoryStack<C, BoxServiceFactory<F, Req>>
into_boxed_service
insteadConvert the factory to factory of BoxedService. Works for MakeService and AsyncMakeService.
sourcepub fn into_boxed_factory(
self,
) -> FactoryStack<C, BoxedMakeService<F::Service, F::Error>>
pub fn into_boxed_factory( self, ) -> FactoryStack<C, BoxedMakeService<F::Service, F::Error>>
Convert the factory to a fixed type factory(Box dyn). Only works for MakeService.
sourcepub fn into_async_boxed_factory(
self,
) -> FactoryStack<C, BoxedAsyncMakeService<F::Service, F::Error>>
pub fn into_async_boxed_factory( self, ) -> FactoryStack<C, BoxedAsyncMakeService<F::Service, F::Error>>
Convert the factory to a fixed type factory(Box dyn). Only works for AsyncMakeService.
sourcepub fn push_boxed_factory(
self,
) -> FactoryStack<C, BoxedMakeService<F::Service, F::Error>>
👎Deprecated: use into_boxed_factory
instead
pub fn push_boxed_factory( self, ) -> FactoryStack<C, BoxedMakeService<F::Service, F::Error>>
into_boxed_factory
insteadConvert the factory to a fixed type factory(Box dyn). Only works for MakeService.
sourcepub fn into_arc_factory(
self,
) -> FactoryStack<C, ArcMakeService<F::Service, F::Error>>
pub fn into_arc_factory( self, ) -> FactoryStack<C, ArcMakeService<F::Service, F::Error>>
Convert the factory to a fixed type factory(Arc dyn). Only works for MakeService.
sourcepub fn into_async_arc_factory(
self,
) -> FactoryStack<C, Arc<BoxedAsyncMakeService<F::Service, F::Error>>>
pub fn into_async_arc_factory( self, ) -> FactoryStack<C, Arc<BoxedAsyncMakeService<F::Service, F::Error>>>
Convert the factory to a fixed type factory(Arc Box dyn). Only works for AsyncMakeService.
sourcepub fn push_arc_factory(
self,
) -> FactoryStack<C, ArcMakeService<F::Service, F::Error>>
👎Deprecated: use into_arc_factory
instead
pub fn push_arc_factory( self, ) -> FactoryStack<C, ArcMakeService<F::Service, F::Error>>
into_arc_factory
insteadConvert the factory to a fixed type factory(Arc dyn). Only works for MakeService.
sourcepub fn check_make_svc<R>(self) -> Self
pub fn check_make_svc<R>(self) -> Self
Check if the stack is a factory of Service<R>
.
sourcepub fn check_async_make_svc<R>(self) -> Self
pub fn check_async_make_svc<R>(self) -> Self
Check if the stack is an async factory of Service<R>
.
sourcepub fn into_inner(self) -> F
pub fn into_inner(self) -> F
Get the inner factory.
sourcepub fn into_parts(self) -> (C, F)
pub fn into_parts(self) -> (C, F)
Into config and the factory.
source§impl<C, F> FactoryStack<C, F>where
F: MakeService,
impl<C, F> FactoryStack<C, F>where
F: MakeService,
source§impl<C, F> FactoryStack<C, F>where
F: AsyncMakeService,
impl<C, F> FactoryStack<C, F>where
F: AsyncMakeService,
sourcepub async fn make_async(&self) -> Result<F::Service, F::Error>
pub async fn make_async(&self) -> Result<F::Service, F::Error>
Make a service in async.