Trait tower::MakeService[][src]

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>>;
    fn poll_ready(
        &mut self,
        cx: &mut Context<'_>
    ) -> Poll<Result<(), Self::MakeError>>;
fn make_service(&mut self, target: Target) -> Self::Future; 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 Services.

Associated Types

Responses given by the service

Errors produced by the service

The Service value created by this factory

Errors produced while building a service.

The future of the Service instance.

Required methods

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.

Create and return a new service value asynchronously.

Provided methods

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();

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();

Implementors