This repository has been archived on 2022-04-04. You can view files and clone it, but cannot push or open issues or pull requests.

108 lines
15 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="A one-shot channel is used for sending a single message between asynchronous tasks. The [`channel`] function is used to create a [`Sender`] and [`Receiver`] handle pair that form the channel."><meta name="keywords" content="rust, rustlang, rust-lang, oneshot"><title>tokio::sync::oneshot - 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" type="text/css" href="../../../normalize.css"><link rel="stylesheet" type="text/css" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script src="../../../crates.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 mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a class="sidebar-logo" href="../../../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.png" alt="logo"></div>
</a><h2 class="location">Module oneshot</h2><div class="sidebar-elems"><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><div id="sidebar-vars" data-name="oneshot" data-ty="mod" data-relpath="./"></div><script defer src="./sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../tokio/index.html"><img class="rust-logo" src="../../../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../../../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><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><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../../../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../../../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Module <a href="../../index.html">tokio</a>::<wbr><a href="../index.html">sync</a>::<wbr><a class="mod" href="#">oneshot</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></span><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">&#x2212;</span>]</a></span><a class="srclink" href="../../../src/tokio/sync/oneshot.rs.html#1-1366" title="goto source code">[src]</a></span></h1><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A one-shot channel is used for sending a single message between
asynchronous tasks. The <a href="fn.channel.html" title="channel"><code>channel</code></a> function is used to create a
<a href="struct.Sender.html" title="Sender"><code>Sender</code></a> and <a href="struct.Receiver.html" title="Receiver"><code>Receiver</code></a> handle pair that form the channel.</p>
<p>The <code>Sender</code> handle is used by the producer to send the value.
The <code>Receiver</code> handle is used by the consumer to receive the value.</p>
<p>Each handle can be used on separate tasks.</p>
<p>Since the <code>send</code> method is not async, it can be used anywhere. This includes
sending between two runtimes, and using it from non-async code.</p>
<h2 id="examples" class="section-header"><a href="#examples">Examples</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">tokio::sync::oneshot</span>;
<span class="attribute">#[<span class="ident">tokio::main</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> (<span class="ident">tx</span>, <span class="ident">rx</span>) <span class="op">=</span> <span class="ident">oneshot::channel</span>();
<span class="ident">tokio::spawn</span>(<span class="kw">async</span> <span class="kw">move</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="kw">_</span>) <span class="op">=</span> <span class="ident">tx</span>.<span class="ident">send</span>(<span class="number">3</span>) {
<span class="macro">println!</span>(<span class="string">&quot;the receiver dropped&quot;</span>);
}
});
<span class="kw">match</span> <span class="ident">rx</span>.<span class="kw">await</span> {
<span class="prelude-val">Ok</span>(<span class="ident">v</span>) =&gt; <span class="macro">println!</span>(<span class="string">&quot;got = {:?}&quot;</span>, <span class="ident">v</span>),
<span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; <span class="macro">println!</span>(<span class="string">&quot;the sender dropped&quot;</span>),
}
}</code></pre></div>
<p>If the sender is dropped without sending, the receiver will fail with
<a href="error/struct.RecvError.html" title="error::RecvError"><code>error::RecvError</code></a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">tokio::sync::oneshot</span>;
<span class="attribute">#[<span class="ident">tokio::main</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> (<span class="ident">tx</span>, <span class="ident">rx</span>) <span class="op">=</span> <span class="ident">oneshot::channel</span>::<span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;</span>();
<span class="ident">tokio::spawn</span>(<span class="kw">async</span> <span class="kw">move</span> {
<span class="ident">drop</span>(<span class="ident">tx</span>);
});
<span class="kw">match</span> <span class="ident">rx</span>.<span class="kw">await</span> {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; <span class="macro">panic!</span>(<span class="string">&quot;This doesn&#39;t happen&quot;</span>),
<span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; <span class="macro">println!</span>(<span class="string">&quot;the sender dropped&quot;</span>),
}
}</code></pre></div>
<p>To use a oneshot channel in a <code>tokio::select!</code> loop, add <code>&amp;mut</code> in front of
the channel.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">tokio::sync::oneshot</span>;
<span class="kw">use</span> <span class="ident">tokio::time</span>::{<span class="ident">interval</span>, <span class="ident">sleep</span>, <span class="ident">Duration</span>};
<span class="attribute">#[<span class="ident">tokio::main</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> (<span class="ident">send</span>, <span class="kw-2">mut</span> <span class="ident">recv</span>) <span class="op">=</span> <span class="ident">oneshot::channel</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">interval</span> <span class="op">=</span> <span class="ident">interval</span>(<span class="ident">Duration::from_millis</span>(<span class="number">100</span>));
<span class="ident">tokio::spawn</span>(<span class="kw">async</span> <span class="kw">move</span> {
<span class="ident">sleep</span>(<span class="ident">Duration::from_secs</span>(<span class="number">1</span>)).<span class="kw">await</span>;
<span class="ident">send</span>.<span class="ident">send</span>(<span class="string">&quot;shut down&quot;</span>).<span class="ident">unwrap</span>();
});
<span class="kw">loop</span> {
<span class="macro">tokio::select!</span> {
<span class="kw">_</span> <span class="op">=</span> <span class="ident">interval</span>.<span class="ident">tick</span>() =&gt; <span class="macro">println!</span>(<span class="string">&quot;Another 100ms&quot;</span>),
<span class="ident">msg</span> <span class="op">=</span> <span class="kw-2">&amp;mut</span> <span class="ident">recv</span> =&gt; {
<span class="macro">println!</span>(<span class="string">&quot;Got message: {}&quot;</span>, <span class="ident">msg</span>.<span class="ident">unwrap</span>());
<span class="kw">break</span>;
}
}
}
}</code></pre></div>
<p>To use a <code>Sender</code> from a destructor, put it in an <a href="https://doc.rust-lang.org/1.59.0/core/option/enum.Option.html" title="Option"><code>Option</code></a> and call
<a href="https://doc.rust-lang.org/1.59.0/core/option/enum.Option.html#method.take" title="Option::take"><code>Option::take</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">tokio::sync::oneshot</span>;
<span class="kw">struct</span> <span class="ident">SendOnDrop</span> {
<span class="ident">sender</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">oneshot::Sender</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> <span class="ident">str</span><span class="op">&gt;</span><span class="op">&gt;</span>,
}
<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">SendOnDrop</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;mut</span> <span class="self">self</span>) {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">sender</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">sender</span>.<span class="ident">take</span>() {
<span class="comment">// Using `let _ =` to ignore send errors.</span>
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="ident">sender</span>.<span class="ident">send</span>(<span class="string">&quot;I got dropped!&quot;</span>);
}
}
}
<span class="attribute">#[<span class="ident">tokio::main</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> (<span class="ident">send</span>, <span class="ident">recv</span>) <span class="op">=</span> <span class="ident">oneshot::channel</span>();
<span class="kw">let</span> <span class="ident">send_on_drop</span> <span class="op">=</span> <span class="ident">SendOnDrop</span> { <span class="ident">sender</span>: <span class="prelude-val">Some</span>(<span class="ident">send</span>) };
<span class="ident">drop</span>(<span class="ident">send_on_drop</span>);
<span class="macro">assert_eq!</span>(<span class="ident">recv</span>.<span class="kw">await</span>, <span class="prelude-val">Ok</span>(<span class="string">&quot;I got dropped!&quot;</span>));
}</code></pre></div>
</div></details><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2>
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="error/index.html" title="tokio::sync::oneshot::error mod">error</a></div><div class="item-right docblock-short"><p>Oneshot error types.</p>
</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2>
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Receiver.html" title="tokio::sync::oneshot::Receiver struct">Receiver</a></div><div class="item-right docblock-short"><p>Receives a value from the associated <a href="struct.Sender.html" title="Sender"><code>Sender</code></a>.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Sender.html" title="tokio::sync::oneshot::Sender struct">Sender</a></div><div class="item-right docblock-short"><p>Sends a value to the associated <a href="struct.Receiver.html" title="Receiver"><code>Receiver</code></a>.</p>
</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2>
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.channel.html" title="tokio::sync::oneshot::channel fn">channel</a></div><div class="item-right docblock-short"><p>Creates a new one-shot channel for sending single values across asynchronous
tasks.</p>
</div></div></div></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="tokio" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
</body></html>