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 Services.

Required Associated Types§

source

type Response

Responses given by the service

source

type Error

Errors produced by the service

source

type Service: Service<Request, Response = Self::Response, Error = Self::Error>

The Service value created by this factory

source

type MakeError

Errors produced while building a service.

source

type Future: Future<Output = Result<Self::Service, Self::MakeError>>

The future of the Service instance.

Required Methods§

source

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.

source

fn make_service(&mut self, target: Target) -> Self::Future

Create and return a new service value asynchronously.

Provided Methods§

source

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

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

Implementors§

source§

impl<M, S, Target, Request> MakeService<Target, Request> for M
where M: Service<Target, Response = S>, S: Service<Request>,

source§

type Response = <S as Service<Request>>::Response

source§

type Error = <S as Service<Request>>::Error

source§

type Service = S

source§

type MakeError = <M as Service<Target>>::Error

source§

type Future = <M as Service<Target>>::Future