Trait MakeService

trait MakeService<Target, Request>: Sealed<(Target, Request)>

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

type Response

Responses given by the service

type Error

Errors produced by the service

type Service: TraitBound { trait_: Path { path: "Service", id: Id(70), args: Some(AngleBracketed { args: [Type(Generic("Request"))], constraints: [AssocItemConstraint { name: "Response", args: None, binding: Equality(Type(QualifiedPath { name: "Response", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(69), args: None }) })) }, AssocItemConstraint { name: "Error", args: None, binding: Equality(Type(QualifiedPath { name: "Error", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(69), args: None }) })) }] }) }, generic_params: [], modifier: None }

The Service value created by this factory

type MakeError

Errors produced while building a service.

type Future: TraitBound { trait_: Path { path: "Future", id: Id(175), args: Some(AngleBracketed { args: [], constraints: [AssocItemConstraint { name: "Output", args: None, binding: Equality(Type(ResolvedPath(Path { path: "Result", id: Id(33), args: Some(AngleBracketed { args: [Type(QualifiedPath { name: "Service", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(69), args: None }) }), Type(QualifiedPath { name: "MakeError", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(69), args: None }) })], constraints: [] }) }))) }] }) }, generic_params: [], modifier: None }

The future of the Service instance.

Required Methods

fn poll_ready(self: &mut Self, cx: &mut Context<'_>) -> Poll<Result<(), <Self as >::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.

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

Create and return a new service value asynchronously.

Provided Methods

fn into_service(self: 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;

# fn main() {
# async {
// 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();
# };
# }
fn as_service(self: &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;

# fn main() {
# async {
// 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