Trait tower::layer::Layer [−][src]
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
pub fn layer(&self, inner: S) -> Self::Service
[src][−]
Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.
Implementations on Foreign Types
impl<'a, T, S> Layer<S> for &'a T where
T: Layer<S> + ?Sized,
[src][−]
T: Layer<S> + ?Sized,
type Service = <T as Layer<S>>::Service
pub fn layer(&self, inner: S) -> <&'a T as Layer<S>>::Service
[src]
Implementors
impl<F, S, Out> Layer<S> for LayerFn<F> where
F: Fn(S) -> Out,
[src][+]
F: Fn(S) -> Out,
impl<P, S> Layer<S> for RetryLayer<P> where
P: Clone,
[src][+]
P: Clone,
impl<S> Layer<S> for Identity
[src][+]
impl<S> Layer<S> for ConcurrencyLimitLayer
[src][+]
impl<S> Layer<S> for RateLimitLayer
[src][+]
impl<S> Layer<S> for LoadShedLayer
[src][+]
impl<S> Layer<S> for TimeoutLayer
[src][+]
impl<S, Inner, Outer> Layer<S> for Stack<Inner, Outer> where
Inner: Layer<S>,
Outer: Layer<<Inner as Layer<S>>::Service>,
[src][+]
Inner: Layer<S>,
Outer: Layer<<Inner as Layer<S>>::Service>,
impl<S, Request> Layer<S> for BufferLayer<Request> where
Request: Send + 'static,
S: Service<Request> + Send + 'static,
<S as Service<Request>>::Future: Send,
<S as Service<Request>>::Error: Into<Box<dyn Error + 'static + Send + Sync, Global>>,
<S as Service<Request>>::Error: Send,
<S as Service<Request>>::Error: Sync,
[src][+]
Request: Send + 'static,
S: Service<Request> + Send + 'static,
<S as Service<Request>>::Future: Send,
<S as Service<Request>>::Error: Into<Box<dyn Error + 'static + Send + Sync, Global>>,
<S as Service<Request>>::Error: Send,
<S as Service<Request>>::Error: Sync,
impl<S, Req> Layer<S> for BalanceLayer<S, Req>
impl<S, Req> Layer<S> for BalanceLayer<S, Req>
impl<S, Request> Layer<S> for BufferLayer<Request> where
S: Service<Request> + Send + 'static,
S::Future: Send,
S::Error: Into<Box<dyn Error + Send + Sync>> + Send + Sync,
Request: Send + 'static,
impl<S, Request> Layer<S> for BufferLayer<Request> where
S: Service<Request> + Send + 'static,
S::Future: Send,
S::Error: Into<Box<dyn Error + Send + Sync>> + Send + Sync,
Request: Send + 'static,
impl<S> Layer<S> for ConcurrencyLimitLayer
impl<S> Layer<S> for ConcurrencyLimitLayer
impl<S> Layer<S> for RateLimitLayer
impl<S> Layer<S> for RateLimitLayer
impl<S> Layer<S> for LoadShedLayer
impl<S> Layer<S> for LoadShedLayer
impl<P, S> Layer<S> for RetryLayer<P> where
P: Clone,
impl<P, S> Layer<S> for RetryLayer<P> where
P: Clone,
impl<S> Layer<S> for TimeoutLayer
impl<S> Layer<S> for TimeoutLayer