mpvrc/crossbeam_channel/macro.select.html

106 lines
No EOL
9 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="Selects from a set of channel operations."><title>select in crossbeam_channel - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.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" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="crossbeam_channel" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../crossbeam_channel/index.html">crossbeam_<wbr>channel</a><span class="version">0.5.13</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 crossbeam_<wbr>channel</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">crossbeam_channel</a></span><h1>Macro <span class="macro">select</span><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/crossbeam_channel/select_macro.rs.html#1111-1121">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>Selects from a set of channel operations.</p>
<p>This macro allows you to define a set of channel operations, wait until any one of them becomes
ready, and finally execute it. If multiple operations are ready at the same time, a random one
among them is selected (i.e. the unbiased selection). Use <code>select_biased!</code> for the biased
selection.</p>
<p>It is also possible to define a <code>default</code> case that gets executed if none of the operations are
ready, either right away or for a certain duration of time.</p>
<p>An operation is considered to be ready if it doesnt have to block. Note that it is ready even
when it will simply return an error because the channel is disconnected.</p>
<p>The <code>select!</code> macro is a convenience wrapper around <a href="struct.Select.html" title="struct crossbeam_channel::Select"><code>Select</code></a>. However, it cannot select over a
dynamically created list of channel operations.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>Block until a send or a receive operation is selected:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>crossbeam_channel::{select, unbounded};
<span class="kw">let </span>(s1, r1) = unbounded();
<span class="kw">let </span>(s2, r2) = unbounded();
s1.send(<span class="number">10</span>).unwrap();
<span class="comment">// Since both operations are initially ready, a random one will be executed.
</span><span class="macro">select!</span> {
recv(r1) -&gt; msg =&gt; <span class="macro">assert_eq!</span>(msg, <span class="prelude-val">Ok</span>(<span class="number">10</span>)),
send(s2, <span class="number">20</span>) -&gt; res =&gt; {
<span class="macro">assert_eq!</span>(res, <span class="prelude-val">Ok</span>(()));
<span class="macro">assert_eq!</span>(r2.recv(), <span class="prelude-val">Ok</span>(<span class="number">20</span>));
}
}</code></pre></div>
<p>Select from a set of operations without blocking:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::thread;
<span class="kw">use </span>std::time::Duration;
<span class="kw">use </span>crossbeam_channel::{select, unbounded};
<span class="kw">let </span>(s1, r1) = unbounded();
<span class="kw">let </span>(s2, r2) = unbounded();
thread::spawn(<span class="kw">move </span>|| {
thread::sleep(Duration::from_secs(<span class="number">1</span>));
s1.send(<span class="number">10</span>).unwrap();
});
thread::spawn(<span class="kw">move </span>|| {
thread::sleep(Duration::from_millis(<span class="number">500</span>));
s2.send(<span class="number">20</span>).unwrap();
});
<span class="comment">// None of the operations are initially ready.
</span><span class="macro">select!</span> {
recv(r1) -&gt; msg =&gt; <span class="macro">panic!</span>(),
recv(r2) -&gt; msg =&gt; <span class="macro">panic!</span>(),
default =&gt; <span class="macro">println!</span>(<span class="string">"not ready"</span>),
}</code></pre></div>
<p>Select over a set of operations with a timeout:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::thread;
<span class="kw">use </span>std::time::Duration;
<span class="kw">use </span>crossbeam_channel::{select, unbounded};
<span class="kw">let </span>(s1, r1) = unbounded();
<span class="kw">let </span>(s2, r2) = unbounded();
thread::spawn(<span class="kw">move </span>|| {
thread::sleep(Duration::from_secs(<span class="number">1</span>));
s1.send(<span class="number">10</span>).unwrap();
});
thread::spawn(<span class="kw">move </span>|| {
thread::sleep(Duration::from_millis(<span class="number">500</span>));
s2.send(<span class="number">20</span>).unwrap();
});
<span class="comment">// None of the two operations will become ready within 100 milliseconds.
</span><span class="macro">select!</span> {
recv(r1) -&gt; msg =&gt; <span class="macro">panic!</span>(),
recv(r2) -&gt; msg =&gt; <span class="macro">panic!</span>(),
default(Duration::from_millis(<span class="number">100</span>)) =&gt; <span class="macro">println!</span>(<span class="string">"timed out"</span>),
}</code></pre></div>
<p>Optionally add a receive operation to <code>select!</code> using <a href="fn.never.html" title="fn crossbeam_channel::never"><code>never</code></a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::thread;
<span class="kw">use </span>std::time::Duration;
<span class="kw">use </span>crossbeam_channel::{select, never, unbounded};
<span class="kw">let </span>(s1, r1) = unbounded();
<span class="kw">let </span>(s2, r2) = unbounded();
thread::spawn(<span class="kw">move </span>|| {
thread::sleep(Duration::from_secs(<span class="number">1</span>));
s1.send(<span class="number">10</span>).unwrap();
});
thread::spawn(<span class="kw">move </span>|| {
thread::sleep(Duration::from_millis(<span class="number">500</span>));
s2.send(<span class="number">20</span>).unwrap();
});
<span class="comment">// This receiver can be a `Some` or a `None`.
</span><span class="kw">let </span>r2 = <span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span>r2);
<span class="comment">// None of the two operations will become ready within 100 milliseconds.
</span><span class="macro">select!</span> {
recv(r1) -&gt; msg =&gt; <span class="macro">panic!</span>(),
recv(r2.unwrap_or(<span class="kw-2">&amp;</span>never())) -&gt; msg =&gt; <span class="macro">assert_eq!</span>(msg, <span class="prelude-val">Ok</span>(<span class="number">20</span>)),
}</code></pre></div>
<p>To optionally add a timeout to <code>select!</code>, see the <a href="fn.never.html#examples" title="fn crossbeam_channel::never">example</a> for <a href="fn.never.html" title="fn crossbeam_channel::never"><code>never</code></a>.</p>
</div></details></section></div></main></body></html>