blob: 4093c311e234859329c856501309b5ed7dae13a4 [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="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" 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="../../../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="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../../tokio/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="../../../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module oneshot</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></section></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">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></h1><span class="out-of-band"><a class="srclink" href="../../../src/tokio/sync/oneshot.rs.html#1-1381">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><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"><a href="#examples">Examples</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::sync::oneshot;
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>(tx, rx) = oneshot::channel();
tokio::spawn(<span class="kw">async move </span>{
<span class="kw">if let </span><span class="prelude-val">Err</span>(<span class="kw">_</span>) = tx.send(<span class="number">3</span>) {
<span class="macro">println!</span>(<span class="string">&quot;the receiver dropped&quot;</span>);
}
});
<span class="kw">match </span>rx.<span class="kw">await </span>{
<span class="prelude-val">Ok</span>(v) =&gt; <span class="macro">println!</span>(<span class="string">&quot;got = {:?}&quot;</span>, v),
<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>tokio::sync::oneshot;
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>(tx, rx) = oneshot::channel::&lt;u32&gt;();
tokio::spawn(<span class="kw">async move </span>{
drop(tx);
});
<span class="kw">match </span>rx.<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>tokio::sync::oneshot;
<span class="kw">use </span>tokio::time::{interval, sleep, Duration};
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>(send, <span class="kw-2">mut </span>recv) = oneshot::channel();
<span class="kw">let </span><span class="kw-2">mut </span>interval = interval(Duration::from_millis(<span class="number">100</span>));
tokio::spawn(<span class="kw">async move </span>{
sleep(Duration::from_secs(<span class="number">1</span>)).<span class="kw">await</span>;
send.send(<span class="string">&quot;shut down&quot;</span>).unwrap();
});
<span class="kw">loop </span>{
<span class="macro">tokio::select! </span>{
<span class="kw">_ </span>= interval.tick() =&gt; <span class="macro">println!</span>(<span class="string">&quot;Another 100ms&quot;</span>),
msg = <span class="kw-2">&amp;mut </span>recv =&gt; {
<span class="macro">println!</span>(<span class="string">&quot;Got message: {}&quot;</span>, msg.unwrap());
<span class="kw">break</span>;
}
}
}
}</code></pre></div>
<p>To use a <code>Sender</code> from a destructor, put it in an [<code>Option</code>] and call
[<code>Option::take</code>].</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::sync::oneshot;
<span class="kw">struct </span>SendOnDrop {
sender: <span class="prelude-ty">Option</span>&lt;oneshot::Sender&lt;<span class="kw-2">&amp;</span><span class="lifetime">&#39;static </span>str&gt;&gt;,
}
<span class="kw">impl </span>Drop <span class="kw">for </span>SendOnDrop {
<span class="kw">fn </span>drop(<span class="kw-2">&amp;mut </span><span class="self">self</span>) {
<span class="kw">if let </span><span class="prelude-val">Some</span>(sender) = <span class="self">self</span>.sender.take() {
<span class="comment">// Using `let _ =` to ignore send errors.
</span><span class="kw">let _ </span>= sender.send(<span class="string">&quot;I got dropped!&quot;</span>);
}
}
}
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>(send, recv) = oneshot::channel();
<span class="kw">let </span>send_on_drop = SendOnDrop { sender: <span class="prelude-val">Some</span>(send) };
drop(send_on_drop);
<span class="macro">assert_eq!</span>(recv.<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">Oneshot error types.</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">Receives a value from the associated <a href="struct.Sender.html" title="Sender"><code>Sender</code></a>.</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">Sends a value to the associated <a href="struct.Receiver.html" title="Receiver"><code>Receiver</code></a>.</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">Creates a new one-shot channel for sending single values across asynchronous
tasks.</div></div></div></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.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>