Files
GopherGate/target/doc/tokio/sync/watch/index.html
2026-02-26 12:00:21 -05:00

122 lines
18 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 multi-producer, multi-consumer channel that only retains the last sent value."><title>tokio::sync::watch - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.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"href="../../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../../" data-static-root-path="../../../static.files/" data-current-crate="tokio" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../../static.files/storage-e2aeef58.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../../static.files/favicon-044be391.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]--><rustdoc-topbar><h2><a href="#">Module watch</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../../tokio/index.html">tokio</a><span class="version">1.49.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Module watch</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#usage" title="Usage">Usage</a><ul><li><a href="#change-notifications" title="Change notifications">Change notifications</a></li><li><a href="#changed-versus-has_changed" title="`changed` versus `has_changed`"><code>changed</code> versus <code>has_changed</code></a></li><li><a href="#borrow_and_update-versus-borrow" title="`borrow_and_update` versus `borrow`"><code>borrow_and_update</code> versus <code>borrow</code></a></li></ul></li><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#closing" title="Closing">Closing</a></li><li><a href="#thread-safety" title="Thread safety">Thread safety</a></li></ul><h3><a href="#modules">Module Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"><h2><a href="../index.html">In tokio::<wbr>sync</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../../index.html">tokio</a>::<wbr><a href="../index.html">sync</a></div><h1>Module <span>watch</span>&nbsp;<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/tokio/sync/watch.rs.html#1-1556">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A multi-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 class="doc-anchor" href="#usage">§</a>Usage</h2>
<p><a href="fn.channel.html" title="fn tokio::sync::watch::channel"><code>channel</code></a> returns a <a href="struct.Sender.html" title="struct tokio::sync::watch::Sender"><code>Sender</code></a> / <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a> pair. These are the producer
and consumer halves of the channel. The channel is created with an initial
value.</p>
<p>Each <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a> independently tracks the last value <em>seen</em> by its caller.</p>
<p>To access the <strong>current</strong> value stored in the channel and mark it as <em>seen</em>
by a given <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a>, use <a href="struct.Receiver.html#method.borrow_and_update" title="method tokio::sync::watch::Receiver::borrow_and_update"><code>Receiver::borrow_and_update()</code></a>.</p>
<p>To access the current value <strong>without</strong> marking it as <em>seen</em>, use
<a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>Receiver::borrow()</code></a>. (If the value has already been marked <em>seen</em>,
<a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>Receiver::borrow()</code></a> is equivalent to <a href="struct.Receiver.html#method.borrow_and_update" title="method tokio::sync::watch::Receiver::borrow_and_update"><code>Receiver::borrow_and_update()</code></a>.)</p>
<p>For more information on when to use these methods, see
<a href="#borrow_and_update-versus-borrow">here</a>.</p>
<h3 id="change-notifications"><a class="doc-anchor" href="#change-notifications">§</a>Change notifications</h3>
<p>The <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a> half provides an asynchronous <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> method. This
method is ready when a new, <em>unseen</em> value is sent via the <a href="struct.Sender.html" title="struct tokio::sync::watch::Sender"><code>Sender</code></a> half.</p>
<ul>
<li><a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>Receiver::changed()</code></a> returns:
<ul>
<li><code>Ok(())</code> on receiving a new value.</li>
<li><code>Err(</code><a href="error/struct.RecvError.html" title="struct tokio::sync::watch::error::RecvError"><code>RecvError</code></a><code>)</code> if the
channel has been closed <strong>AND</strong> the current value is <em>seen</em>.</li>
</ul>
</li>
<li>If the current value is <em>unseen</em> when calling <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a>, then
<a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> will return immediately. If the current value is <em>seen</em>, then
it will sleep until either a new message is sent via the <a href="struct.Sender.html" title="struct tokio::sync::watch::Sender"><code>Sender</code></a> half,
or the <a href="struct.Sender.html" title="struct tokio::sync::watch::Sender"><code>Sender</code></a> is dropped.</li>
<li>On completion, the <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> method marks the new value as <em>seen</em>.</li>
<li>At creation, the initial value is considered <em>seen</em>. In other words,
<a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>Receiver::changed()</code></a> will not return until a subsequent value is sent.</li>
<li>New <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a> instances can be created with <a href="struct.Sender.html#method.subscribe" title="method tokio::sync::watch::Sender::subscribe"><code>Sender::subscribe()</code></a>.
The current value at the time the <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a> is created is considered
<em>seen</em>.</li>
</ul>
<h3 id="changed-versus-has_changed"><a class="doc-anchor" href="#changed-versus-has_changed">§</a><a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> versus <a href="struct.Receiver.html#method.has_changed" title="method tokio::sync::watch::Receiver::has_changed"><code>has_changed</code></a></h3>
<p>The <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a> half provides two methods for checking for changes
in the channel, <a href="struct.Receiver.html#method.has_changed" title="method tokio::sync::watch::Receiver::has_changed"><code>has_changed</code></a> and <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a>.</p>
<ul>
<li>
<p><a href="struct.Receiver.html#method.has_changed" title="method tokio::sync::watch::Receiver::has_changed"><code>has_changed</code></a> is a <em>synchronous</em> method that checks whether the current
value is seen or not and returns a boolean. This method does <strong>not</strong> mark the
value as seen.</p>
</li>
<li>
<p><a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> is an <em>asynchronous</em> method that will return once an unseen
value is in the channel. This method does mark the value as seen.</p>
</li>
</ul>
<p>Note there are two behavioral differences on when these two methods return
an error.</p>
<ul>
<li><a href="struct.Receiver.html#method.has_changed" title="method tokio::sync::watch::Receiver::has_changed"><code>has_changed</code></a> errors if and only if the channel is closed.</li>
<li><a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> errors if the channel has been closed <strong>AND</strong>
the current value is seen.</li>
</ul>
<p>See the example below that shows how these methods have different fallibility.</p>
<h3 id="borrow_and_update-versus-borrow"><a class="doc-anchor" href="#borrow_and_update-versus-borrow">§</a><a href="struct.Receiver.html#method.borrow_and_update" title="method tokio::sync::watch::Receiver::borrow_and_update"><code>borrow_and_update</code></a> versus <a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>borrow</code></a></h3>
<p>If the receiver intends to await notifications from <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> in a loop,
<a href="struct.Receiver.html#method.borrow_and_update" title="method tokio::sync::watch::Receiver::borrow_and_update"><code>Receiver::borrow_and_update()</code></a> should be preferred over
<a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>Receiver::borrow()</code></a>. This avoids a potential race where a new value is
sent between <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> being ready and the value being read. (If
<a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>Receiver::borrow()</code></a> is used, the loop may run twice with the same value.)</p>
<p>If the receiver is only interested in the current value, and does not intend
to wait for changes, then <a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>Receiver::borrow()</code></a> can be used. It may be more
convenient to use <a href="struct.Receiver.html#method.borrow" title="method tokio::sync::watch::Receiver::borrow"><code>borrow</code></a> since its an <code>&amp;self</code>
method—<a href="struct.Receiver.html#method.borrow_and_update" title="method tokio::sync::watch::Receiver::borrow_and_update"><code>borrow_and_update</code></a> requires <code>&amp;mut self</code>.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>The following example prints <code>hello! world! </code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::sync::watch;
<span class="kw">use </span>tokio::time::{Duration, sleep};
<span class="kw">let </span>(tx, <span class="kw-2">mut </span>rx) = watch::channel(<span class="string">"hello"</span>);
tokio::spawn(<span class="kw">async move </span>{
<span class="comment">// Use the equivalent of a "do-while" loop so the initial value is
// processed before awaiting the `changed()` future.
</span><span class="kw">loop </span>{
<span class="macro">println!</span>(<span class="string">"{}! "</span>, <span class="kw-2">*</span>rx.borrow_and_update());
<span class="kw">if </span>rx.changed().<span class="kw">await</span>.is_err() {
<span class="kw">break</span>;
}
}
});
sleep(Duration::from_millis(<span class="number">100</span>)).<span class="kw">await</span>;
tx.send(<span class="string">"world"</span>)<span class="question-mark">?</span>;</code></pre></div>
<p>Difference on fallibility of <a href="struct.Receiver.html#method.changed" title="method tokio::sync::watch::Receiver::changed"><code>changed</code></a> versus <a href="struct.Receiver.html#method.has_changed" title="method tokio::sync::watch::Receiver::has_changed"><code>has_changed</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::sync::watch;
<span class="attr">#[tokio::main(flavor = <span class="string">"current_thread"</span>)]
</span><span class="kw">let </span>(tx, <span class="kw-2">mut </span>rx) = watch::channel(<span class="string">"hello"</span>);
tx.send(<span class="string">"goodbye"</span>).unwrap();
drop(tx);
<span class="comment">// `has_changed` does not mark the value as seen and errors
// since the channel is closed.
</span><span class="macro">assert!</span>(rx.has_changed().is_err());
<span class="comment">// `changed` returns Ok since the value is not already marked as seen
// even if the channel is closed.
</span><span class="macro">assert!</span>(rx.changed().<span class="kw">await</span>.is_ok());
<span class="comment">// The `changed` call above marks the value as seen.
// The next `changed` call now returns an error as the channel is closed
// AND the current value is seen.
</span><span class="macro">assert!</span>(rx.changed().<span class="kw">await</span>.is_err());</code></pre></div><h2 id="closing"><a class="doc-anchor" href="#closing">§</a>Closing</h2>
<p><a href="struct.Sender.html#method.is_closed" title="method tokio::sync::watch::Sender::is_closed"><code>Sender::is_closed</code></a> and <a href="struct.Sender.html#method.closed" title="method tokio::sync::watch::Sender::closed"><code>Sender::closed</code></a> allow the producer to detect
when all <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><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 all senders and all
receivers have been dropped.</p>
<h2 id="thread-safety"><a class="doc-anchor" href="#thread-safety">§</a>Thread safety</h2>
<p>Both <a href="struct.Sender.html" title="struct tokio::sync::watch::Sender"><code>Sender</code></a> and <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><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" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a>
handles may be moved to separate threads and also used concurrently.</p>
</div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="error/index.html" title="mod tokio::sync::watch::error">error</a></dt><dd>Watch error types.</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver">Receiver</a></dt><dd>Receives values from the associated <a href="struct.Sender.html" title="struct tokio::sync::watch::Sender"><code>Sender</code></a>.</dd><dt><a class="struct" href="struct.Ref.html" title="struct tokio::sync::watch::Ref">Ref</a></dt><dd>Returns a reference to the inner value.</dd><dt><a class="struct" href="struct.Sender.html" title="struct tokio::sync::watch::Sender">Sender</a></dt><dd>Sends values to the associated <a href="struct.Receiver.html" title="struct tokio::sync::watch::Receiver"><code>Receiver</code></a>.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.channel.html" title="fn tokio::sync::watch::channel">channel</a></dt><dd>Creates a new watch channel, returning the “send” and “receive” handles.</dd></dl></section></div></main></body></html>