Trait tower::MakeService
source · pub trait MakeService<Target, Request>: Sealed<(Target, Request)> {
type Response;
type Error;
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
type MakeError;
type Future: Future<Output = Result<Self::Service, Self::MakeError>>;
// Required methods
fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::MakeError>>;
fn make_service(&mut self, target: Target) -> Self::Future;
// Provided methods
fn into_service(self) -> IntoService<Self, Request>
where Self: Sized { ... }
fn as_service(&mut self) -> AsService<'_, Self, Request>
where Self: Sized { ... }
}
Expand description
Creates new Service
values.
Acts as a service factory. This is useful for cases where new Service
values must be produced. One case is a TCP server listener. The listener
accepts new TCP streams, obtains a new Service
value using the
MakeService
trait, and uses that new Service
value to process inbound
requests on that new TCP stream.
This is essentially a trait alias for a Service
of Service
s.
Required Associated Types§
Required Methods§
sourcefn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), Self::MakeError>>
fn poll_ready( &mut self, cx: &mut Context<'_> ) -> Poll<Result<(), Self::MakeError>>
Returns Poll::Ready
when the factory is able to create more services.
If the service is at capacity, then Poll::Pending
is returned and the task
is notified when the service becomes ready again. This function is
expected to be called while on a task.
sourcefn make_service(&mut self, target: Target) -> Self::Future
fn make_service(&mut self, target: Target) -> Self::Future
Create and return a new service value asynchronously.
Provided Methods§
sourcefn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
Consume this MakeService
and convert it into a Service
.
§Example
use std::convert::Infallible;
use tower::Service;
use tower::make::MakeService;
use tower::service_fn;
// A `MakeService`
let make_service = service_fn(|make_req: ()| async {
Ok::<_, Infallible>(service_fn(|req: String| async {
Ok::<_, Infallible>(req)
}))
});
// Convert the `MakeService` into a `Service`
let mut svc = make_service.into_service();
// Make a new service
let mut new_svc = svc.call(()).await.unwrap();
// Call the service
let res = new_svc.call("foo".to_string()).await.unwrap();
sourcefn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
Convert this MakeService
into a Service
without consuming the original MakeService
.
§Example
use std::convert::Infallible;
use tower::Service;
use tower::make::MakeService;
use tower::service_fn;
// A `MakeService`
let mut make_service = service_fn(|make_req: ()| async {
Ok::<_, Infallible>(service_fn(|req: String| async {
Ok::<_, Infallible>(req)
}))
});
// Convert the `MakeService` into a `Service`
let mut svc = make_service.as_service();
// Make a new service
let mut new_svc = svc.call(()).await.unwrap();
// Call the service
let res = new_svc.call("foo".to_string()).await.unwrap();
// The original `MakeService` is still accessible
let new_svc = make_service.make_service(()).await.unwrap();