Struct FutureService
pub struct FutureService<F, S> { /* private fields */ }
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) -> SelfReturns a new
FutureServicefor the given future.A
FutureServiceallows 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
UnpinboundThe
Unpinbound onFis necessary because the future will be polled inService::poll_readywhich doesn't have a pinned receiver (it takes&mut selfand notself: Pin<&mut Self>). So we cannot put the future into aPinwithout requiringUnpin.This will most likely come up if you're calling
future_servicewith an async block. In that case you can useBox::pin(async { ... })as shown in the example.
Trait Implementations
impl<F, S> Debug for FutureService<F, S>
where
S: Debug,
fn fmt(&self, f: &mut Formatter<'_>) -> Result
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 as Service<R>>::Response;type Error = E;type Future = <S as Service<R>>::Future;fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>fn call(&mut self, req: R) -> Self::Future
impl<F: Clone, S: Clone> Clone for FutureService<F, S>
fn clone(&self) -> Self