blob: fc0ebdb83c3abae706c31fb921b1c783a274f469 [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 single-producer, multi-consumer channel that only retains the last sent value."><meta name="keywords" content="rust, rustlang, rust-lang, watch"><title>tokio::sync::watch - 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 watch</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="#">watch</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/watch.rs.html#1-1231">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 single-producer, multi-consumer channel that only retains the <em>last</em> sent
value.</p>
<p>This channel is useful for watching for changes to a value from multiple
points in the code base, for example, changes to configuration values.</p>
<h2 id="usage"><a href="#usage">Usage</a></h2>
<p><a href="fn.channel.html"><code>channel</code></a> returns a <a href="struct.Sender.html"><code>Sender</code></a> / <a href="struct.Receiver.html"><code>Receiver</code></a> pair. These are the producer
and consumer halves of the channel. The channel is created with an initial
value. The <strong>latest</strong> value stored in the channel is accessed with
<a href="struct.Receiver.html#method.borrow"><code>Receiver::borrow()</code></a>. Awaiting <a href="struct.Receiver.html#method.changed"><code>Receiver::changed()</code></a> waits for a new
value to be sent by the <a href="struct.Sender.html"><code>Sender</code></a> half.</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::watch;
<span class="kw">let </span>(tx, <span class="kw-2">mut </span>rx) = watch::channel(<span class="string">&quot;hello&quot;</span>);
tokio::spawn(<span class="kw">async move </span>{
<span class="kw">while </span>rx.changed().<span class="kw">await</span>.is_ok() {
<span class="macro">println!</span>(<span class="string">&quot;received = {:?}&quot;</span>, <span class="kw-2">*</span>rx.borrow());
}
});
tx.send(<span class="string">&quot;world&quot;</span>)<span class="question-mark">?</span>;</code></pre></div>
<h2 id="closing"><a href="#closing">Closing</a></h2>
<p><a href="struct.Sender.html#method.is_closed"><code>Sender::is_closed</code></a> and <a href="struct.Sender.html#method.closed"><code>Sender::closed</code></a> allow the producer to detect
when all <a href="struct.Receiver.html"><code>Receiver</code></a> handles have been dropped. This indicates that there
is no further interest in the values being produced and work can be stopped.</p>
<p>The value in the channel will not be dropped until the sender and all receivers
have been dropped.</p>
<h2 id="thread-safety"><a href="#thread-safety">Thread safety</a></h2>
<p>Both <a href="struct.Sender.html"><code>Sender</code></a> and <a href="struct.Receiver.html"><code>Receiver</code></a> are thread safe. They can be moved to other
threads and can be used in a concurrent environment. Clones of <a href="struct.Receiver.html"><code>Receiver</code></a>
handles may be moved to separate threads and also used concurrently.</p>
</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::watch::error mod">error</a></div><div class="item-right docblock-short">Watch 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::watch::Receiver struct">Receiver</a></div><div class="item-right docblock-short">Receives values from the associated <a href="struct.Sender.html"><code>Sender</code></a>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Ref.html" title="tokio::sync::watch::Ref struct">Ref</a></div><div class="item-right docblock-short">Returns a reference to the inner value.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Sender.html" title="tokio::sync::watch::Sender struct">Sender</a></div><div class="item-right docblock-short">Sends values to the associated <a href="struct.Receiver.html"><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::watch::channel fn">channel</a></div><div class="item-right docblock-short">Creates a new watch channel, returning the “send” and “receive” handles.</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>