pub struct ServiceChain<S, St, Req> { /* private fields */ }Expand description
A builder for composing services and combinators into one service.
Created by service() and service combinators. Each builder method
consumes the chain and returns a new chain containing the added adapter.
Use Self::and_then to process successful responses, Self::then to
handle the complete result, or Self::apply_fn to wrap a call with
asynchronous middleware.
The chain implements Service when its enclosed composition does.
It owns the enclosed service but does not store pipeline state; supply
state through Service::pipeline.
Binding, calls, readiness, and shutdown are forwarded to the enclosed
service. The builder adds no independent readiness cache. Raw children
share the enclosing readiness scope; use crate::ready() around a child
that needs its own cache and call-time readiness check.
Cloning clones the enclosed service. Shared state and readiness depend on that service’s clone implementation.
See the framework guide’s Service Pipelines chapter for composition and readiness semantics.
Implementations§
Source§impl<S, St, Req> ServiceChain<S, St, Req>where
S: Service<St, Req>,
impl<S, St, Req> ServiceChain<S, St, Req>where
S: Service<St, Req>,
Sourcepub fn and_then<Next, F>(
self,
service: F,
) -> ServiceChain<AndThen<S, Next>, St, Req>where
ServiceChain<S, St, Req>: Sized,
F: IntoService<Next, St, <S as Service<St, Req>>::Res>,
Next: Service<St, <S as Service<St, Req>>::Res>,
pub fn and_then<Next, F>(
self,
service: F,
) -> ServiceChain<AndThen<S, Next>, St, Req>where
ServiceChain<S, St, Req>: Sized,
F: IntoService<Next, St, <S as Service<St, Req>>::Res>,
Next: Service<St, <S as Service<St, Req>>::Res>,
Calls another service after this service completes successfully.
The current service’s response becomes the next service’s request. If the current service returns an error, the next service is not called. Otherwise, the next service’s result becomes the chain’s result.
Both services must use the same state and error types for the resulting
chain to implement Service. Binding, readiness, and shutdown cover
both children. See AndThen for readiness behavior between calls.
Sourcepub fn then<Next, F>(self, service: F) -> ServiceChain<Then<S, Next>, St, Req>
pub fn then<Next, F>(self, service: F) -> ServiceChain<Then<S, Next>, St, Req>
Calls another service after this service completes.
The next service receives the current service’s complete Result, so it
can handle either a response or an error.
Both services must use the same state and error types for the resulting
chain to implement Service. The next service determines the final
response or error. Errors from the chain’s readiness check are returned
directly, not passed to the next service as a request.
See Then for an error-recovery example and lifecycle behavior.
Sourcepub fn map<F, Res>(self, f: F) -> ServiceChain<Map<F, S, Res>, St, Req>
pub fn map<F, Res>(self, f: F) -> ServiceChain<Map<F, S, Res>, St, Req>
Maps this service’s response to a different type.
Like Result::map, this invokes the synchronous function only for
successful responses. Errors, requests, state, and lifecycle behavior
are unchanged. Use Self::and_then for asynchronous processing.
Sourcepub fn map_err<F, Err>(self, f: F) -> ServiceChain<MapErr<F, S, Err>, St, Req>
pub fn map_err<F, Err>(self, f: F) -> ServiceChain<MapErr<F, S, Err>, St, Req>
Maps this service’s error to a different type.
The synchronous function transforms errors from both calls and readiness checks, making this useful for normalizing error types before composing services. Successful responses, requests, and state are unchanged. Binding and shutdown are forwarded without modification.
Sourcepub fn readiness<F>(
self,
ready: F,
) -> ServiceChain<AndThen<S, FnReadiness<F, <S as Service<St, Req>>::Error>>, St, Req>
pub fn readiness<F>( self, ready: F, ) -> ServiceChain<AndThen<S, FnReadiness<F, <S as Service<St, Req>>::Error>>, St, Req>
Adds a custom readiness check to the service chain.
The callback and the wrapped service’s readiness futures are polled together; the callback does not wait for the service to become ready. The chain is ready only when both succeed. Requests and responses pass through unchanged.
The callback must implement Clone for the resulting chain to
implement Service. A successful check can be cached by the enclosing
pipeline, so this is not a per-request callback.
The callback receives only pipeline state. For scoped readiness and
change notifications, implement Service directly.
Sourcepub fn shutdown<F>(
self,
sh: F,
) -> ServiceChain<AndThen<S, FnShutdown<F, <S as Service<St, Req>>::Error>>, St, Req>
pub fn shutdown<F>( self, sh: F, ) -> ServiceChain<AndThen<S, FnShutdown<F, <S as Service<St, Req>>::Error>>, St, Req>
Adds a callback invoked at most once when the service shuts down.
The callback receives pipeline state and is polled alongside the wrapped service’s shutdown future, not after it completes. Shutdown waits for both. Requests and responses are unchanged.
The callback is consumed when first polled; cancellation does not make it available for a later shutdown attempt. Dropping the chain alone does not invoke it.
Sourcepub fn apply_fn<F, In, Out, Err>(
self,
f: F,
) -> ServiceChain<Apply<S, St, Req, F, In, Out, Err>, St, In>
pub fn apply_fn<F, In, Out, Err>( self, f: F, ) -> ServiceChain<Apply<S, St, Req, F, In, Out, Err>, St, In>
Applies an asynchronous function as middleware to this service.
This is shorthand for calling crate::apply_fn on the chained service.
The function receives an input request and an ApplyCtx for state
access and inner calls. It may change request, response, and error types,
or decide not to call the inner service.
State is unchanged. Inner readiness errors are converted with From;
errors from ApplyCtx::call retain their inner type. See
crate::apply_fn for call-time readiness behavior.
Trait Implementations§
Source§impl<S, St, Req> Clone for ServiceChain<S, St, Req>
impl<S, St, Req> Clone for ServiceChain<S, St, Req>
Source§fn clone(&self) -> ServiceChain<S, St, Req>
fn clone(&self) -> ServiceChain<S, St, Req>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more