pub trait MakeService<Target, Request>: Sealed<(Target, Request)> {
    type Response;
    type Error;
    type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
    type MakeError;

    // Required method
    fn make_service(
        &self,
        target: Target
    ) -> impl Future<Output = Result<Self::Service, Self::MakeError>>;

    // Provided methods
    fn into_service(self) -> IntoService<Self, Request>
       where Self: Sized { ... }
    fn as_service(&self) -> AsService<'_, Self, Request>
       where Self: Sized { ... }
}
Available on crate feature make only.
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.

Required Methods§

source

fn make_service( &self, target: Target ) -> impl Future<Output = Result<Self::Service, Self::MakeError>>

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_async::Service;
use tower_async::make::MakeService;
use tower_async::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(&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_async::Service;
use tower_async::make::MakeService;
use tower_async::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();

Object Safety§

This trait is not object safe.

Implementors§

source§

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

§

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

§

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

§

type Service = S

§

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