Files
google-apis-rs/futures/macro.select.html
2024-03-05 21:06:01 +01:00

118 lines
11 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 - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="futures" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.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></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../futures/index.html">futures</a><span class="version">0.3.28</span></h2></div><div class="sidebar-elems"></div></nav><div class="sidebar-resizer"></div>
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../futures/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Macro <a href="index.html">futures</a>::<wbr><a class="macro" href="#">select</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/futures_util/async_await/select_mod.rs.html#318">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><pre class="rust item-decl"><span class="macro">macro_rules! </span>select {
($(<span class="macro-nonterminal">$tokens</span>:tt)<span class="kw-2">*</span>) =&gt; { ... };
}</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 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;
<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_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;
<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! </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>