Struct tower::util::FutureService[][src]

pub struct FutureService<F, S> { /* fields omitted */ }
This is supported on crate feature util only.

A type that implements Service for a Future that produces a Service.

See future_service for more details.


impl<F, S> FutureService<F, S>[src]

pub fn new(future: F) -> Self[src]

Returns a new FutureService for the given future.

A FutureService allows you to treat a future that resolves to a service as a service. This can be useful for services that are created asynchronously.


use tower::{service_fn, Service, ServiceExt};
use tower::util::FutureService;
use std::convert::Infallible;

// A future which outputs a type implementing `Service`.
let future_of_a_service = async {
    let svc = service_fn(|_req: ()| async { Ok::<_, Infallible>("ok") });
    Ok::<_, Infallible>(svc)

// Wrap the future with a `FutureService`, allowing it to be used
// as a service without awaiting the future's completion:
let mut svc = FutureService::new(Box::pin(future_of_a_service));

// Now, when we wait for the service to become ready, it will
// drive the future to completion internally.
let svc = svc.ready().await.unwrap();
let res =;

Regarding the Unpin bound

The Unpin bound on F is necessary because the future will be polled in Service::poll_ready which doesn’t have a pinned receiver (it takes &mut self and not self: Pin<&mut Self>). So we cannot put the future into a Pin without requiring Unpin.

This will most likely come up if you’re calling future_service with an async block. In that case you can use Box::pin(async { ... }) as shown in the example.

Trait Implementations

impl<F: Clone, S: Clone> Clone for FutureService<F, S>[src]

impl<F, S> Debug for FutureService<F, S> where
    S: Debug

impl<F, S, R, E> Service<R> for FutureService<F, S> where
    F: Future<Output = Result<S, E>> + Unpin,
    S: Service<R, Error = E>, 

type Response = S::Response

Responses given by the service.

type Error = E

Errors produced by the service.

type Future = S::Future

The future response value.

Auto Trait Implementations

impl<F, S> RefUnwindSafe for FutureService<F, S> where
    F: RefUnwindSafe,
    S: RefUnwindSafe

impl<F, S> Send for FutureService<F, S> where
    F: Send,
    S: Send

impl<F, S> Sync for FutureService<F, S> where
    F: Sync,
    S: Sync

impl<F, S> Unpin for FutureService<F, S> where
    F: Unpin,
    S: Unpin

impl<F, S> UnwindSafe for FutureService<F, S> where
    F: UnwindSafe,
    S: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized

impl<T> Borrow<T> for T where
    T: ?Sized

impl<T> BorrowMut<T> for T where
    T: ?Sized

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 

impl<T, Request> ServiceExt<Request> for T where
    T: Service<Request> + ?Sized

impl<T> ToOwned for T where
    T: Clone

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,