Expand description
Decorates a Service
, transforming either the request or the response.
Often, many of the pieces needed for writing network applications can be
reused across multiple services. The Layer
trait can be used to write
reusable components that can be applied to very different kinds of services;
for example, it can be applied to services operating on different protocols,
and to both the client and server side of a network transaction.
Log
Take request logging as an example:
pub struct LogLayer {
target: &'static str,
}
impl<S> Layer<S> for LogLayer {
type Service = LogService<S>;
fn layer(&self, service: S) -> Self::Service {
LogService {
target: self.target,
service
}
}
}
// This service implements the Log behavior
pub struct LogService<S> {
target: &'static str,
service: S,
}
impl<S, Request> Service<Request> for LogService<S>
where
S: Service<Request>,
Request: fmt::Debug,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, request: Request) -> Self::Future {
// Insert log statement here or other functionality
println!("request = {:?}, target = {:?}", request, self.target);
self.service.call(request)
}
}
The above log implementation is decoupled from the underlying protocol and is also decoupled from client or server concerns. In other words, the same log middleware could be used in either a client or a server.
Associated Types
Required methods
Implementations on Foreign Types
Implementors
sourceimpl<In, T, U, E> Layer<In> for BoxLayer<In, T, U, E>
This is supported on crate feature util
only.
impl<In, T, U, E> Layer<In> for BoxLayer<In, T, U, E>
This is supported on crate feature
util
only.type Service = BoxService<T, U, E>
sourceimpl<P, S> Layer<S> for RetryLayer<P> where
P: Clone,
This is supported on crate feature retry
only.
impl<P, S> Layer<S> for RetryLayer<P> where
P: Clone,
This is supported on crate feature
retry
only.sourceimpl<S> Layer<S> for Identity
impl<S> Layer<S> for Identity
Decorates a Service
, transforming either the request or the response.
type Service = S
sourceimpl<S> Layer<S> for ConcurrencyLimitLayer
This is supported on crate feature limit
only.
impl<S> Layer<S> for ConcurrencyLimitLayer
This is supported on crate feature
limit
only.type Service = ConcurrencyLimit<S>
sourceimpl<S> Layer<S> for GlobalConcurrencyLimitLayer
This is supported on crate feature limit
only.
impl<S> Layer<S> for GlobalConcurrencyLimitLayer
This is supported on crate feature
limit
only.type Service = ConcurrencyLimit<S>
sourceimpl<S> Layer<S> for RateLimitLayer
This is supported on crate feature limit
only.
impl<S> Layer<S> for RateLimitLayer
This is supported on crate feature
limit
only.sourceimpl<S> Layer<S> for LoadShedLayer
This is supported on crate feature load-shed
only.
impl<S> Layer<S> for LoadShedLayer
This is supported on crate feature
load-shed
only.sourceimpl<S> Layer<S> for SpawnReadyLayer
This is supported on crate feature spawn-ready
only.
impl<S> Layer<S> for SpawnReadyLayer
This is supported on crate feature
spawn-ready
only.type Service = MakeSpawnReady<S>
sourceimpl<S> Layer<S> for TimeoutLayer
This is supported on crate feature timeout
only.
impl<S> Layer<S> for TimeoutLayer
This is supported on crate feature
timeout
only.sourceimpl<S, A, B> Layer<S> for Either<A, B> where
A: Layer<S>,
B: Layer<S>,
This is supported on crate feature util
only.
impl<S, A, B> Layer<S> for Either<A, B> where
A: Layer<S>,
B: Layer<S>,
This is supported on crate feature
util
only.sourceimpl<S, F> Layer<S> for AndThenLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for AndThenLayer<F> where
F: Clone,
This is supported on crate feature
util
only.sourceimpl<S, F> Layer<S> for MapErrLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for MapErrLayer<F> where
F: Clone,
This is supported on crate feature
util
only.sourceimpl<S, F> Layer<S> for MapFutureLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for MapFutureLayer<F> where
F: Clone,
This is supported on crate feature
util
only.sourceimpl<S, F> Layer<S> for MapRequestLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for MapRequestLayer<F> where
F: Clone,
This is supported on crate feature
util
only.type Service = MapRequest<S, F>
sourceimpl<S, F> Layer<S> for MapResponseLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for MapResponseLayer<F> where
F: Clone,
This is supported on crate feature
util
only.type Service = MapResponse<S, F>
sourceimpl<S, F> Layer<S> for MapResultLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for MapResultLayer<F> where
F: Clone,
This is supported on crate feature
util
only.sourceimpl<S, F> Layer<S> for ThenLayer<F> where
F: Clone,
This is supported on crate feature util
only.
impl<S, F> Layer<S> for ThenLayer<F> where
F: Clone,
This is supported on crate feature
util
only.sourceimpl<S, Inner, Outer> Layer<S> for Stack<Inner, Outer> where
Inner: Layer<S>,
Outer: Layer<<Inner as Layer<S>>::Service>,
impl<S, Inner, Outer> Layer<S> for Stack<Inner, Outer> where
Inner: Layer<S>,
Outer: Layer<<Inner as Layer<S>>::Service>,
sourceimpl<S, Req> Layer<S> for MakeBalanceLayer<S, Req>
This is supported on crate feature balance
only.
impl<S, Req> Layer<S> for MakeBalanceLayer<S, Req>
This is supported on crate feature
balance
only.type Service = MakeBalance<S, Req>
sourceimpl<S, Request> Layer<S> for BufferLayer<Request> where
S: Service<Request> + Send + 'static,
S::Future: Send,
S::Error: Into<BoxError> + Send + Sync,
Request: Send + 'static,
This is supported on crate feature buffer
only.
impl<S, Request> Layer<S> for BufferLayer<Request> where
S: Service<Request> + Send + 'static,
S::Future: Send,
S::Error: Into<BoxError> + Send + Sync,
Request: Send + 'static,
This is supported on crate feature
buffer
only.sourceimpl<U: Clone, S> Layer<S> for AsyncFilterLayer<U>
This is supported on crate feature filter
only.
impl<U: Clone, S> Layer<S> for AsyncFilterLayer<U>
This is supported on crate feature
filter
only.