blob: b7392583583a746fc4a7c57b8720113ba1767169 [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="Polls multiple futures and streams simultaneously, executing the branch for the future that finishes first. Unlike `select!`, if multiple futures are ready, one will be selected in order of declaration. Futures directly passed to `select_biased!` must be `Unpin` and implement `FusedFuture`."><meta name="keywords" content="rust, rustlang, rust-lang, select_biased"><title>select_biased in futures_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 macro"><!--[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="../futures_util/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="../futures_util/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 futures_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">Macro <a href="index.html">futures_util</a>::<wbr><a class="macro" href="#">select_biased</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/futures_util/async_await/select_mod.rs.html#328-335">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"><div class="example-wrap"><pre class="rust macro"><code><span class="macro">macro_rules! </span>select_biased {
($(<span class="macro-nonterminal">$tokens</span>:tt)<span class="kw-2">*</span>) =&gt; { ... };
}</code></pre></div>
</div><details class="rustdoc-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. Unlike <a href="macro.select.html"><code>select!</code></a>, if multiple futures are ready,
one will be selected in order of declaration. Futures directly
passed to <code>select_biased!</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_biased!</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_biased!</code> will not be enough to prevent it from being
polled after completion if the <code>select_biased!</code> call is in a loop, so when
<code>select_biased!</code>ing in a loop, users should take care to <code>fuse()</code> outside of
the loop.</p>
<p><code>select_biased!</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_biased!</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 href="#examples">Examples</a></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_biased;
<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_biased! </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_biased;
<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_biased! </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_biased</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_biased;
<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_biased! </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">12</span>);</code></pre></div>
<p>If a similar async function is called outside of <code>select_biased</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_biased</code>. This can be achieved via <code>Box::pin</code> for pinning a
<code>Future</code> on the heap or the <code>pin_mut!</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>futures::future::FutureExt;
<span class="kw">use </span>futures::select_biased;
<span class="kw">use </span>futures::pin_mut;
<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="macro">pin_mut!</span>(fut_2); <span class="comment">// Pins the Future on the stack
</span><span class="kw">let </span>res = <span class="macro">select_biased! </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_biased</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_biased!</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_biased;
<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_biased! </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_biased!</code> block’s 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><div id="rustdoc-vars" data-root-path="../" data-current-crate="futures_util" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>