68 lines
6.5 KiB
HTML
68 lines
6.5 KiB
HTML
<!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 Futures channel-like utility to signal when a value is wanted."><title>want - 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="want" 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="../crates.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 crate"><!--[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="#">Crate want</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../want/index.html">want</a><span class="version">0.3.1</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"></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"><h1>Crate <span>want</span> <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/want/lib.rs.html#1-579">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A Futures channel-like utility to signal when a value is wanted.</p>
|
||
<p>Futures are supposed to be lazy, and only starting work if <code>Future::poll</code>
|
||
is called. The same is true of <code>Stream</code>s, but when using a channel as
|
||
a <code>Stream</code>, it can be hard to know if the receiver is ready for the next
|
||
value.</p>
|
||
<p>Put another way, given a <code>(tx, rx)</code> from <code>futures::sync::mpsc::channel()</code>,
|
||
how can the sender (<code>tx</code>) know when the receiver (<code>rx</code>) actually wants more
|
||
work to be produced? Just because there is room in the channel buffer
|
||
doesn’t mean the work would be used by the receiver.</p>
|
||
<p>This is where something like <code>want</code> comes in. Added to a channel, you can
|
||
make sure that the <code>tx</code> only creates the message and sends it when the <code>rx</code>
|
||
has <code>poll()</code> for it, and the buffer was empty.</p>
|
||
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2><div class="example-wrap"><pre class="language-nightly"><code># //#![feature(async_await)]
|
||
extern crate want;
|
||
|
||
# fn spawn<T>(_t: T) {}
|
||
# fn we_still_want_message() -> bool { true }
|
||
# fn mpsc_channel() -> (Tx, Rx) { (Tx, Rx) }
|
||
# struct Tx;
|
||
# impl Tx { fn send<T>(&mut self, _: T) {} }
|
||
# struct Rx;
|
||
# impl Rx { async fn recv(&mut self) -> Option<Expensive> { Some(Expensive) } }
|
||
|
||
// Some message that is expensive to produce.
|
||
struct Expensive;
|
||
|
||
// Some futures-aware MPSC channel...
|
||
let (mut tx, mut rx) = mpsc_channel();
|
||
|
||
// And our `want` channel!
|
||
let (mut gv, mut tk) = want::new();
|
||
|
||
|
||
// Our receiving task...
|
||
spawn(async move {
|
||
// Maybe something comes up that prevents us from ever
|
||
// using the expensive message.
|
||
//
|
||
// Without `want`, the "send" task may have started to
|
||
// produce the expensive message even though we wouldn't
|
||
// be able to use it.
|
||
if !we_still_want_message() {
|
||
return;
|
||
}
|
||
|
||
// But we can use it! So tell the `want` channel.
|
||
tk.want();
|
||
|
||
match rx.recv().await {
|
||
Some(_msg) => println!("got a message"),
|
||
None => println!("DONE"),
|
||
}
|
||
});
|
||
|
||
// Our sending task
|
||
spawn(async move {
|
||
// It's expensive to create a new message, so we wait until the
|
||
// receiving end truly *wants* the message.
|
||
if let Err(_closed) = gv.want().await {
|
||
// Looks like they will never want it...
|
||
return;
|
||
}
|
||
|
||
// They want it, let's go!
|
||
tx.send(Expensive);
|
||
});
|
||
|
||
# fn main() {}</code></pre></div></div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Closed.html" title="struct want::Closed">Closed</a></dt><dd>The <code>Taker</code> has canceled its interest in a value.</dd><dt><a class="struct" href="struct.Giver.html" title="struct want::Giver">Giver</a></dt><dd>An entity that gives a value when wanted.</dd><dt><a class="struct" href="struct.SharedGiver.html" title="struct want::SharedGiver">Shared<wbr>Giver</a></dt><dd>A cloneable <code>Giver</code>.</dd><dt><a class="struct" href="struct.Taker.html" title="struct want::Taker">Taker</a></dt><dd>An entity that wants a value.</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.new.html" title="fn want::new">new</a></dt><dd>Create a new <code>want</code> channel.</dd></dl></section></div></main></body></html> |