blob: a79926e9fc3a35c2edc16d3729551df4daf2a714 [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Returns a new [`FutureService`] for the given future."><meta name="keywords" content="rust, rustlang, rust-lang, future_service"><title>future_service in tower::util - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In tower::util</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="../index.html">tower</a>::<wbr><a href="index.html">util</a>::<wbr><a class="fn" href="#">future_service</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/tower/util/future_service.rs.html#48-54">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn future_service&lt;F, S, R, E&gt;(future: F) -&gt; <a class="struct" href="struct.FutureService.html" title="struct tower::util::FutureService">FutureService</a>&lt;F, S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: Future&lt;Output = Result&lt;S, E&gt;&gt; + Unpin,<br>&nbsp;&nbsp;&nbsp;&nbsp;S: <a class="trait" href="../trait.Service.html" title="trait tower::Service">Service</a>&lt;R, Error = E&gt;,</span></code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Returns a new <a href="struct.FutureService.html" title="FutureService"><code>FutureService</code></a> for the given future.</p>
<p>A <a href="struct.FutureService.html" title="FutureService"><code>FutureService</code></a> allows you to treat a future that resolves to a service as a service. This
can be useful for services that are created asynchronously.</p>
<h2 id="example"><a href="#example">Example</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tower::{service_fn, Service, ServiceExt};
<span class="kw">use </span>tower::util::future_service;
<span class="kw">use </span>std::convert::Infallible;
<span class="comment">// A future which outputs a type implementing `Service`.
</span><span class="kw">let </span>future_of_a_service = <span class="kw">async </span>{
<span class="kw">let </span>svc = service_fn(|_req: ()| <span class="kw">async </span>{ <span class="prelude-val">Ok</span>::&lt;<span class="kw">_</span>, Infallible&gt;(<span class="string">&quot;ok&quot;</span>) });
<span class="prelude-val">Ok</span>::&lt;<span class="kw">_</span>, Infallible&gt;(svc)
};
<span class="comment">// Wrap the future with a `FutureService`, allowing it to be used
// as a service without awaiting the future&#39;s completion:
</span><span class="kw">let </span><span class="kw-2">mut </span>svc = future_service(Box::pin(future_of_a_service));
<span class="comment">// Now, when we wait for the service to become ready, it will
// drive the future to completion internally.
</span><span class="kw">let </span>svc = svc.ready().<span class="kw">await</span>.unwrap();
<span class="kw">let </span>res = svc.call(()).<span class="kw">await</span>.unwrap();</code></pre></div>
<h2 id="regarding-the-unpin-bound"><a href="#regarding-the-unpin-bound">Regarding the [<code>Unpin</code>] bound</a></h2>
<p>The [<code>Unpin</code>] bound on <code>F</code> is necessary because the future will be polled in
<a href="../trait.Service.html#tymethod.poll_ready" title="Service::poll_ready"><code>Service::poll_ready</code></a> which doesn’t have a pinned receiver (it takes <code>&amp;mut self</code> and not <code>self: Pin&lt;&amp;mut Self&gt;</code>). So we cannot put the future into a <code>Pin</code> without requiring <code>Unpin</code>.</p>
<p>This will most likely come up if you’re calling <code>future_service</code> with an async block. In that
case you can use <code>Box::pin(async { ... })</code> as shown in the example.</p>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="tower" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>