Files
GopherGate/target/doc/futures_util/macro.select.html
2026-02-26 12:00:21 -05:00

117 lines
10 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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="Polls multiple futures and streams simultaneously, executing the branch for the future that finishes first. If multiple futures are ready, one will be pseudo-randomly selected at runtime. Futures directly passed to `select!` must be `Unpin` and implement `FusedFuture`."><title>select in futures_util - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="futures_util" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">select</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../futures_util/index.html">futures_<wbr>util</a><span class="version">0.3.32</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">select</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate futures_<wbr>util</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">futures_util</a></div><h1>Macro <span class="macro">select</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/futures_util/async_await/select_mod.rs.html#322-329">Source</a> </span></div><pre class="rust item-decl"><code>macro_rules! select {
($($tokens:tt)*) =&gt; { ... };
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Polls multiple futures and streams simultaneously, executing the branch
for the future that finishes first. If multiple futures are ready,
one will be pseudo-randomly selected at runtime. Futures directly
passed to <code>select!</code> must be <code>Unpin</code> and implement <code>FusedFuture</code>.</p>
<p>If an expression which yields a <code>Future</code> is passed to <code>select!</code>
(e.g. an <code>async fn</code> call) instead of a <code>Future</code> by name the <code>Unpin</code>
requirement is relaxed, since the macro will pin the resulting <code>Future</code>
on the stack. However the <code>Future</code> returned by the expression must
still implement <code>FusedFuture</code>.</p>
<p>Futures and streams which are not already fused can be fused using the
<code>.fuse()</code> method. Note, though, that fusing a future or stream directly
in the call to <code>select!</code> will not be enough to prevent it from being
polled after completion if the <code>select!</code> call is in a loop, so when
<code>select!</code>ing in a loop, users should take care to <code>fuse()</code> outside of
the loop.</p>
<p><code>select!</code> can be used as an expression and will return the return
value of the selected branch. For this reason the return type of every
branch in a <code>select!</code> must be the same.</p>
<p>This macro is only usable inside of async functions, closures, and blocks.
It is also gated behind the <code>async-await</code> feature of this library, which is
activated by default.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future;
<span class="kw">use </span>futures::select;
<span class="kw">let </span><span class="kw-2">mut </span>a = future::ready(<span class="number">4</span>);
<span class="kw">let </span><span class="kw-2">mut </span>b = future::pending::&lt;()&gt;();
<span class="kw">let </span>res = <span class="macro">select!</span> {
a_res = a =&gt; a_res + <span class="number">1</span>,
<span class="kw">_ </span>= b =&gt; <span class="number">0</span>,
};
<span class="macro">assert_eq!</span>(res, <span class="number">5</span>);</code></pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future;
<span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt};
<span class="kw">use </span>futures::select;
<span class="kw">let </span><span class="kw-2">mut </span>st = stream::iter(<span class="macro">vec!</span>[<span class="number">2</span>]).fuse();
<span class="kw">let </span><span class="kw-2">mut </span>fut = future::pending::&lt;()&gt;();
<span class="macro">select!</span> {
x = st.next() =&gt; <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="number">2</span>), x),
<span class="kw">_ </span>= fut =&gt; <span class="macro">panic!</span>(),
}</code></pre></div>
<p>As described earlier, <code>select</code> can directly select on expressions
which return <code>Future</code>s - even if those do not implement <code>Unpin</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future::FutureExt;
<span class="kw">use </span>futures::select;
<span class="comment">// Calling the following async fn returns a Future which does not
// implement Unpin
</span><span class="kw">async fn </span>async_identity_fn(arg: usize) -&gt; usize {
arg
}
<span class="kw">let </span>res = <span class="macro">select!</span> {
a_res = async_identity_fn(<span class="number">62</span>).fuse() =&gt; a_res + <span class="number">1</span>,
b_res = async_identity_fn(<span class="number">13</span>).fuse() =&gt; b_res,
};
<span class="macro">assert!</span>(res == <span class="number">63 </span>|| res == <span class="number">13</span>);</code></pre></div>
<p>If a similar async function is called outside of <code>select</code> to produce
a <code>Future</code>, the <code>Future</code> must be pinned in order to be able to pass
it to <code>select</code>. This can be achieved via <code>Box::pin</code> for pinning a
<code>Future</code> on the heap or the <code>pin!</code> macro for pinning a <code>Future</code>
on the stack.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>core::pin::pin;
<span class="kw">use </span>futures::future::FutureExt;
<span class="kw">use </span>futures::select;
<span class="comment">// Calling the following async fn returns a Future which does not
// implement Unpin
</span><span class="kw">async fn </span>async_identity_fn(arg: usize) -&gt; usize {
arg
}
<span class="kw">let </span>fut_1 = async_identity_fn(<span class="number">1</span>).fuse();
<span class="kw">let </span>fut_2 = async_identity_fn(<span class="number">2</span>).fuse();
<span class="kw">let </span><span class="kw-2">mut </span>fut_1 = Box::pin(fut_1); <span class="comment">// Pins the Future on the heap
</span><span class="kw">let </span><span class="kw-2">mut </span>fut_2 = <span class="macro">pin!</span>(fut_2); <span class="comment">// Pins the Future on the stack
</span><span class="kw">let </span>res = <span class="macro">select!</span> {
a_res = fut_1 =&gt; a_res,
b_res = fut_2 =&gt; b_res,
};
<span class="macro">assert!</span>(res == <span class="number">1 </span>|| res == <span class="number">2</span>);</code></pre></div>
<p><code>select</code> also accepts a <code>complete</code> branch and a <code>default</code> branch.
<code>complete</code> will run if all futures and streams have already been
exhausted. <code>default</code> will run if no futures or streams are
immediately ready. <code>complete</code> takes priority over <code>default</code> in
the case where all futures have completed.
A motivating use-case for passing <code>Future</code>s by name as well as for
<code>complete</code> blocks is to call <code>select!</code> in a loop, which is
demonstrated in the following example:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future;
<span class="kw">use </span>futures::select;
<span class="kw">let </span><span class="kw-2">mut </span>a_fut = future::ready(<span class="number">4</span>);
<span class="kw">let </span><span class="kw-2">mut </span>b_fut = future::ready(<span class="number">6</span>);
<span class="kw">let </span><span class="kw-2">mut </span>total = <span class="number">0</span>;
<span class="kw">loop </span>{
<span class="macro">select!</span> {
a = a_fut =&gt; total += a,
b = b_fut =&gt; total += b,
complete =&gt; <span class="kw">break</span>,
default =&gt; <span class="macro">panic!</span>(), <span class="comment">// never runs (futures run first, then complete)
</span>}
}
<span class="macro">assert_eq!</span>(total, <span class="number">10</span>);</code></pre></div>
<p>Note that the futures that have been matched over can still be mutated
from inside the <code>select!</code> blocks branches. This can be used to implement
more complex behavior such as timer resets or writing into the head of
a stream.</p>
</div></details></section></div></main></body></html>