Struct FutureService

struct FutureService<F, S> { ... }

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

See future_service for more details.

Implementations

impl<F, S> FutureService<F, S>

const fn new(future: F) -> Self

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.

Example

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

# fn main() {
# async {
// 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 = svc.call(()).await.unwrap();
# };
# }

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.

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

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<F, S> Freeze for FutureService<F, S>

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

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

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

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

impl<F, S> UnsafeUnpin for FutureService<F, S>

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

impl<F, S, R, E> Service for FutureService<F, S>

fn poll_ready(self: &mut Self, cx: &mut Context<'_>) -> Poll<Result<(), <Self as >::Error>>
fn call(self: &mut Self, req: R) -> <Self as >::Future

impl<F: $crate::clone::Clone, S: $crate::clone::Clone> Clone for FutureService<F, S>

fn clone(self: &Self) -> FutureService<F, S>

impl<M, S, Target, Request> MakeService for FutureService<F, S>

fn poll_ready(self: &mut Self, cx: &mut Context<'_>) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn make_service(self: &mut Self, target: Target) -> <M as MakeService<Target, Request>>::Future

impl<T> Any for FutureService<F, S>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for FutureService<F, S>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for FutureService<F, S>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for FutureService<F, S>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for FutureService<F, S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for FutureService<F, S>

impl<T> ToOwned for FutureService<F, S>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> WithSubscriber for FutureService<F, S>

impl<T, Request> ServiceExt for FutureService<F, S>

impl<T, U> Into for FutureService<F, S>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for FutureService<F, S>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for FutureService<F, S>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>