|
|
|
|
<!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="API documentation for the Rust `channel` mod in crate `crossbeam`."><meta name="keywords" content="rust, rustlang, rust-lang, channel"><title>crossbeam::channel - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../crossbeam/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Module channel</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>crossbeam</a></p><script>window.sidebarCurrent = {name: 'channel', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/crossbeam_channel/lib.rs.html#1-372' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>crossbeam</a>::<wbr><a class="mod" href=''>channel</a></span></h1><div class='docblock'><p>Multi-producer multi-consumer channels for message passing.</p>
|
|
|
|
|
<p>This crate is an alternative to <a href="https://doc.rust-lang.org/std/sync/mpsc/index.html"><code>std::sync::mpsc</code></a> with more features and better performance.</p>
|
|
|
|
|
<h1 id="hello-world" class="section-header"><a href="#hello-world">Hello, world!</a></h1>
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">unbounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Create a channel of unbounded capacity.</span>
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Send a message into the channel.</span>
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="string">"Hello, world!"</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Receive the message from the channel.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="string">"Hello, world!"</span>));</pre></div>
|
|
|
|
|
<h1 id="channel-types" class="section-header"><a href="#channel-types">Channel types</a></h1>
|
|
|
|
|
<p>Channels can be created using two functions:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
|
|
|
|
<p><a href="fn.bounded.html"><code>bounded</code></a> creates a channel of bounded capacity, i.e. there is a limit to how many messages
|
|
|
|
|
it can hold at a time.</p>
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
<p><a href="fn.unbounded.html"><code>unbounded</code></a> creates a channel of unbounded capacity, i.e. it can hold any number of
|
|
|
|
|
messages at a time.</p>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>Both functions return a <a href="struct.Sender.html"><code>Sender</code></a> and a <a href="struct.Receiver.html"><code>Receiver</code></a>, which represent the two opposite sides
|
|
|
|
|
of a channel.</p>
|
|
|
|
|
<p>Creating a bounded channel:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">bounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Create a channel that can hold at most 5 messages at a time.</span>
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">bounded</span>(<span class="number">5</span>);
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Can send only 5 messages without blocking.</span>
|
|
|
|
|
<span class="kw">for</span> <span class="ident">i</span> <span class="kw">in</span> <span class="number">0</span>..<span class="number">5</span> {
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="ident">i</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Another call to `send` would block because the channel is full.</span>
|
|
|
|
|
<span class="comment">// s.send(5).unwrap();</span></pre></div>
|
|
|
|
|
<p>Creating an unbounded channel:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">unbounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Create an unbounded channel.</span>
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Can send any number of messages into the channel without blocking.</span>
|
|
|
|
|
<span class="kw">for</span> <span class="ident">i</span> <span class="kw">in</span> <span class="number">0</span>..<span class="number">1000</span> {
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="ident">i</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
}</pre></div>
|
|
|
|
|
<p>A special case is zero-capacity channel, which cannot hold any messages. Instead, send and
|
|
|
|
|
receive operations must appear at the same time in order to pair up and pass the message over:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">bounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Create a zero-capacity channel.</span>
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">bounded</span>(<span class="number">0</span>);
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Sending blocks until a receive operation appears on the other side.</span>
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">s</span>.<span class="ident">send</span>(<span class="string">"Hi!"</span>).<span class="ident">unwrap</span>());
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Receiving blocks until a send operation appears on the other side.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="string">"Hi!"</span>));</pre></div>
|
|
|
|
|
<h1 id="sharing-channels" class="section-header"><a href="#sharing-channels">Sharing channels</a></h1>
|
|
|
|
|
<p>Senders and receivers can be cloned and sent to other threads:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">bounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s1</span>, <span class="ident">r1</span>) <span class="op">=</span> <span class="ident">bounded</span>(<span class="number">0</span>);
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s2</span>, <span class="ident">r2</span>) <span class="op">=</span> (<span class="ident">s1</span>.<span class="ident">clone</span>(), <span class="ident">r1</span>.<span class="ident">clone</span>());
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Spawn a thread that receives a message and then sends one.</span>
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
|
|
|
|
|
<span class="ident">r2</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s2</span>.<span class="ident">send</span>(<span class="number">2</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Send a message and then receive one.</span>
|
|
|
|
|
<span class="ident">s1</span>.<span class="ident">send</span>(<span class="number">1</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">r1</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>();</pre></div>
|
|
|
|
|
<p>Note that cloning only creates a new handle to the same sending or receiving side. It does not
|
|
|
|
|
create a separate stream of messages in any way:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">unbounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s1</span>, <span class="ident">r1</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s2</span>, <span class="ident">r2</span>) <span class="op">=</span> (<span class="ident">s1</span>.<span class="ident">clone</span>(), <span class="ident">r1</span>.<span class="ident">clone</span>());
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s3</span>, <span class="ident">r3</span>) <span class="op">=</span> (<span class="ident">s2</span>.<span class="ident">clone</span>(), <span class="ident">r2</span>.<span class="ident">clone</span>());
|
|
|
|
|
|
|
|
|
|
<span class="ident">s1</span>.<span class="ident">send</span>(<span class="number">10</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s2</span>.<span class="ident">send</span>(<span class="number">20</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s3</span>.<span class="ident">send</span>(<span class="number">30</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r3</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="number">10</span>));
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r1</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="number">20</span>));
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r2</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="number">30</span>));</pre></div>
|
|
|
|
|
<p>It's also possible to share senders and receivers by reference:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">bounded</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_utils</span>::<span class="ident">thread</span>::<span class="ident">scope</span>;
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">bounded</span>(<span class="number">0</span>);
|
|
|
|
|
|
|
|
|
|
<span class="ident">scope</span>(<span class="op">|</span><span class="ident">scope</span><span class="op">|</span> {
|
|
|
|
|
<span class="comment">// Spawn a thread that receives a message and then sends one.</span>
|
|
|
|
|
<span class="ident">scope</span>.<span class="ident">spawn</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> {
|
|
|
|
|
<span class="ident">r</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">2</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Send a message and then receive one.</span>
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">1</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">r</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>();
|
|
|
|
|
}).<span class="ident">unwrap</span>();</pre></div>
|
|
|
|
|
<h1 id="disconnection" class="section-header"><a href="#disconnection">Disconnection</a></h1>
|
|
|
|
|
<p>When all senders or all receivers associated with a channel get dropped, the channel becomes
|
|
|
|
|
disconnected. No more messages can be sent, but any remaining messages can still be received.
|
|
|
|
|
Send and receive operations on a disconnected channel never block.</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::{<span class="ident">unbounded</span>, <span class="ident">RecvError</span>};
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">1</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">2</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">3</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
|
|
|
|
|
<span class="comment">// The only sender is dropped, disconnecting the channel.</span>
|
|
|
|
|
<span class="ident">drop</span>(<span class="ident">s</span>);
|
|
|
|
|
|
|
|
|
|
<span class="comment">// The remaining messages can be received.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="number">1</span>));
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="number">2</span>));
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="number">3</span>));
|
|
|
|
|
|
|
|
|
|
<span class="comment">// There are no more messages in the channel.</span>
|
|
|
|
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">is_empty</span>());
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Note that calling `r.recv()` does not block.</span>
|
|
|
|
|
<span class="comment">// Instead, `Err(RecvError)` is returned immediately.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Err</span>(<span class="ident">RecvError</span>));</pre></div>
|
|
|
|
|
<h1 id="blocking-operations" class="section-header"><a href="#blocking-operations">Blocking operations</a></h1>
|
|
|
|
|
<p>Send and receive operations come in three flavors:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>Non-blocking (returns immediately with success or failure).</li>
|
|
|
|
|
<li>Blocking (waits until the operation succeeds or the channel becomes disconnected).</li>
|
|
|
|
|
<li>Blocking with a timeout (blocks only for a certain duration of time).</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>A simple example showing the difference between non-blocking and blocking operations:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::{<span class="ident">bounded</span>, <span class="ident">RecvError</span>, <span class="ident">TryRecvError</span>};
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">bounded</span>(<span class="number">1</span>);
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Send a message into the channel.</span>
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="string">"foo"</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
|
|
|
|
|
<span class="comment">// This call would block because the channel is full.</span>
|
|
|
|
|
<span class="comment">// s.send("bar").unwrap();</span>
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Receive the message.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Ok</span>(<span class="string">"foo"</span>));
|
|
|
|
|
|
|
|
|
|
<span class="comment">// This call would block because the channel is empty.</span>
|
|
|
|
|
<span class="comment">// r.recv();</span>
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Try receiving a message without blocking.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">try_recv</span>(), <span class="prelude-val">Err</span>(<span class="ident">TryRecvError</span>::<span class="ident">Empty</span>));
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Disconnect the channel.</span>
|
|
|
|
|
<span class="ident">drop</span>(<span class="ident">s</span>);
|
|
|
|
|
|
|
|
|
|
<span class="comment">// This call doesn't block because the channel is now disconnected.</span>
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">recv</span>(), <span class="prelude-val">Err</span>(<span class="ident">RecvError</span>));</pre></div>
|
|
|
|
|
<h1 id="iteration" class="section-header"><a href="#iteration">Iteration</a></h1>
|
|
|
|
|
<p>Receivers can be used as iterators. For example, method <a href="struct.Receiver.html#method.iter"><code>iter</code></a> creates an iterator that
|
|
|
|
|
receives messages until the channel becomes empty and disconnected. Note that iteration may
|
|
|
|
|
block waiting for next message to arrive.</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">unbounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">1</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">2</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">3</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">drop</span>(<span class="ident">s</span>); <span class="comment">// Disconnect the channel.</span>
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Collect all messages from the channel.</span>
|
|
|
|
|
<span class="comment">// Note that the call to `collect` blocks until the sender is dropped.</span>
|
|
|
|
|
<span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw">_</span><span class="op">></span> <span class="op">=</span> <span class="ident">r</span>.<span class="ident">iter</span>().<span class="ident">collect</span>();
|
|
|
|
|
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</pre></div>
|
|
|
|
|
<p>A non-blocking iterator can be created using <a href="struct.Receiver.html#method.try_iter"><code>try_iter</code></a>, which receives all available
|
|
|
|
|
messages without blocking:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">unbounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s</span>, <span class="ident">r</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">1</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">2</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="ident">s</span>.<span class="ident">send</span>(<span class="number">3</span>).<span class="ident">unwrap</span>();
|
|
|
|
|
<span class="comment">// No need to drop the sender.</span>
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Receive all messages currently in the channel.</span>
|
|
|
|
|
<span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw">_</span><span class="op">></span> <span class="op">=</span> <span class="ident">r</span>.<span class="ident">try_iter</span>().<span class="ident">collect</span>();
|
|
|
|
|
|
|
|
|
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</pre></div>
|
|
|
|
|
<h1 id="selection" class="section-header"><a href="#selection">Selection</a></h1>
|
|
|
|
|
<p>The <a href="macro.select.html"><code>select!</code></a> 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.</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 doesn't have to block. Note that it is ready even
|
|
|
|
|
when it will simply return an error because the channel is disconnected.</p>
|
|
|
|
|
<p>An example of receiving a message from two channels:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::<span class="ident">Duration</span>;
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::<span class="ident">unbounded</span>;
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s1</span>, <span class="ident">r1</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
<span class="kw">let</span> (<span class="ident">s2</span>, <span class="ident">r2</span>) <span class="op">=</span> <span class="ident">unbounded</span>();
|
|
|
|
|
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">s1</span>.<span class="ident">send</span>(<span class="number">10</span>).<span class="ident">unwrap</span>());
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">s2</span>.<span class="ident">send</span>(<span class="number">20</span>).<span class="ident">unwrap</span>());
|
|
|
|
|
|
|
|
|
|
<span class="comment">// At most one of these two receive operations will be executed.</span>
|
|
|
|
|
<span class="macro">select</span><span class="macro">!</span> {
|
|
|
|
|
<span class="ident">recv</span>(<span class="ident">r1</span>) <span class="op">-</span><span class="op">></span> <span class="ident">msg</span> <span class="op">=</span><span class="op">></span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">msg</span>, <span class="prelude-val">Ok</span>(<span class="number">10</span>)),
|
|
|
|
|
<span class="ident">recv</span>(<span class="ident">r2</span>) <span class="op">-</span><span class="op">></span> <span class="ident">msg</span> <span class="op">=</span><span class="op">></span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">msg</span>, <span class="prelude-val">Ok</span>(<span class="number">20</span>)),
|
|
|
|
|
<span class="ident">default</span>(<span class="ident">Duration</span>::<span class="ident">from_secs</span>(<span class="number">1</span>)) <span class="op">=</span><span class="op">></span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"timed out"</span>),
|
|
|
|
|
}</pre></div>
|
|
|
|
|
<p>If you need to select over a dynamically created list of channel operations, use <a href="struct.Select.html"><code>Select</code></a>
|
|
|
|
|
instead. The <a href="macro.select.html"><code>select!</code></a> macro is just a convenience wrapper around <a href="struct.Select.html"><code>Select</code></a>.</p>
|
|
|
|
|
<h1 id="extra-channels" class="section-header"><a href="#extra-channels">Extra channels</a></h1>
|
|
|
|
|
<p>Three functions can create special kinds of channels, all of which return just a <a href="struct.Receiver.html"><code>Receiver</code></a>
|
|
|
|
|
handle:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="fn.after.html"><code>after</code></a> creates a channel that delivers a single message after a certain duration of time.</li>
|
|
|
|
|
<li><a href="fn.tick.html"><code>tick</code></a> creates a channel that delivers messages periodically.</li>
|
|
|
|
|
<li><a href="fn.never.html"><code>never</code></a> creates a channel that never delivers messages.</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>These channels are very efficient because messages get lazily generated on receive operations.</p>
|
|
|
|
|
<p>An example that prints elapsed time every 50 milliseconds for the duration of 1 second:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::{<span class="ident">Duration</span>, <span class="ident">Instant</span>};
|
|
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_channel</span>::{<span class="ident">after</span>, <span class="ident">tick</span>};
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="ident">Instant</span>::<span class="ident">now</span>();
|
|
|
|
|
<span class="kw">let</span> <span class="ident">ticker</span> <span class="op">=</span> <span class="ident">tick</span>(<span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">50</span>));
|
|
|
|
|
<span class="kw">let</span> <span class="ident">timeout</span> <span class="op">=</span> <span class="ident">after</span>(<span class="ident">Duration</span>::<span class="ident">from_secs</span>(<span class="number">1</span>));
|
|
|
|
|
|
|
|
|
|
<span class="kw">loop</span> {
|
|
|
|
|
<span class="macro">select</span><span class="macro">!</span> {
|
|
|
|
|
<span class="ident">recv</span>(<span class="ident">ticker</span>) <span class="op">-</span><span class="op">></span> <span class="kw">_</span> <span class="op">=</span><span class="op">></span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"elapsed: {:?}"</span>, <span class="ident">start</span>.<span class="ident">elapsed</span>()),
|
|
|
|
|
<span class="ident">recv</span>(<span class="ident">timeout</span>) <span class="op">-</span><span class="op">></span> <span class="kw">_</span> <span class="op">=</span><span class="op">></span> <span class="kw">break</span>,
|
|
|
|
|
}
|
|
|
|
|
}</pre></div>
|
|
|
|
|
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="macro" href="macro.select.html" title='crossbeam::channel::select macro'>select</a></td><td class='docblock-short'><p>Selects from a set of channel operations.</p>
|
|
|
|
|
</td></tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="struct" href="struct.IntoIter.html" title='crossbeam::channel::IntoIter struct'>IntoIter</a></td><td class='docblock-short'><p>A blocking iterator over messages in a channel.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Iter.html" title='crossbeam::channel::Iter struct'>Iter</a></td><td class='docblock-short'><p>A blocking iterator over messages in a channel.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.ReadyTimeoutError.html" title='crossbeam::channel::ReadyTimeoutError struct'>ReadyTimeoutError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Select.html#method.ready_timeout"><code>ready_timeout</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Receiver.html" title='crossbeam::channel::Receiver struct'>Receiver</a></td><td class='docblock-short'><p>The receiving side of a channel.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.RecvError.html" title='crossbeam::channel::RecvError struct'>RecvError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Receiver.html#method.recv"><code>recv</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Select.html" title='crossbeam::channel::Select struct'>Select</a></td><td class='docblock-short'><p>Selects from a set of channel operations.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.SelectTimeoutError.html" title='crossbeam::channel::SelectTimeoutError struct'>SelectTimeoutError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Select.html#method.select_timeout"><code>select_timeout</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.SelectedOperation.html" title='crossbeam::channel::SelectedOperation struct'>SelectedOperation</a></td><td class='docblock-short'><p>A selected operation that needs to be completed.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.SendError.html" title='crossbeam::channel::SendError struct'>SendError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Sender.html#method.send"><code>send</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Sender.html" title='crossbeam::channel::Sender struct'>Sender</a></td><td class='docblock-short'><p>The sending side of a channel.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.TryIter.html" title='crossbeam::channel::TryIter struct'>TryIter</a></td><td class='docblock-short'><p>A non-blocking iterator over messages in a channel.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.TryReadyError.html" title='crossbeam::channel::TryReadyError struct'>TryReadyError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Select.html#method.try_ready"><code>try_ready</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.TrySelectError.html" title='crossbeam::channel::TrySelectError struct'>TrySelectError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Select.html#method.try_select"><code>try_select</code></a> method.</p>
|
|
|
|
|
</td></tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="enum" href="enum.RecvTimeoutError.html" title='crossbeam::channel::RecvTimeoutError enum'>RecvTimeoutError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Receiver.html#method.recv_timeout"><code>recv_timeout</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="enum" href="enum.SendTimeoutError.html" title='crossbeam::channel::SendTimeoutError enum'>SendTimeoutError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Sender.html#method.send_timeout"><code>send_timeout</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="enum" href="enum.TryRecvError.html" title='crossbeam::channel::TryRecvError enum'>TryRecvError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Receiver.html#method.recv"><code>try_recv</code></a> method.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="enum" href="enum.TrySendError.html" title='crossbeam::channel::TrySendError enum'>TrySendError</a></td><td class='docblock-short'><p>An error returned from the <a href="struct.Sender.html#method.try_send"><code>try_send</code></a> method.</p>
|
|
|
|
|
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="fn" href="fn.after.html" title='crossbeam::channel::after fn'>after</a></td><td class='docblock-short'><p>Creates a receiver that delivers a message after a certain duration of time.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="fn" href="fn.bounded.html" title='crossbeam::channel::bounded fn'>bounded</a></td><td class='docblock-short'><p>Creates a channel of bounded capacity.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="fn" href="fn.never.html" title='crossbeam::channel::never fn'>never</a></td><td class='docblock-short'><p>Creates a receiver that never delivers messages.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="fn" href="fn.tick.html" title='crossbeam::channel::tick fn'>tick</a></td><td class='docblock-short'><p>Creates a receiver that delivers messages periodically.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="fn" href="fn.unbounded.html" title='crossbeam::channel::unbounded fn'>unbounded</a></td><td class='docblock-short'><p>Creates a channel of unbounded capacity.</p>
|
|
|
|
|
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "crossbeam";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>
|