blob: 9791484c34653740ea2ae13030de07d094f07c6d [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="Asynchronous stream of elements."><meta name="keywords" content="rust, rustlang, rust-lang, async_stream"><title>async_stream - 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="../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 crate"><!--[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="../async_stream/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="../async_stream/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate async_stream</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.3.5</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#macros">Macros</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">Crate <a class="mod" href="#">async_stream</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/async_stream/lib.rs.html#1-242">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>Asynchronous stream of elements.</p>
<p>Provides two macros, <code>stream!</code> and <code>try_stream!</code>, allowing the caller to
define asynchronous streams of elements. These are implemented using <code>async</code>
&amp; <code>await</code> notation. This crate works without unstable features.</p>
<p>The <code>stream!</code> macro returns an anonymous type implementing the <a href="https://docs.rs/futures-core/*/futures_core/stream/trait.Stream.html"><code>Stream</code></a>
trait. The <code>Item</code> associated type is the type of the values yielded from the
stream. The <code>try_stream!</code> also returns an anonymous type implementing the
<a href="https://docs.rs/futures-core/*/futures_core/stream/trait.Stream.html"><code>Stream</code></a> trait, but the <code>Item</code> associated type is <code>Result&lt;T, Error&gt;</code>. The
<code>try_stream!</code> macro supports using <code>?</code> notation as part of the
implementation.</p>
<h2 id="usage"><a href="#usage">Usage</a></h2>
<p>A basic stream yielding numbers. Values are yielded using the <code>yield</code>
keyword. The stream block must return <code>()</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>async_stream::stream;
<span class="kw">use </span>futures_util::pin_mut;
<span class="kw">use </span>futures_util::stream::StreamExt;
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>s = <span class="macro">stream! </span>{
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">3 </span>{
<span class="kw">yield </span>i;
}
};
<span class="macro">pin_mut!</span>(s); <span class="comment">// needed for iteration
</span><span class="kw">while let </span><span class="prelude-val">Some</span>(value) = s.next().<span class="kw">await </span>{
<span class="macro">println!</span>(<span class="string">&quot;got {}&quot;</span>, value);
}
}</code></pre></div>
<p>Streams may be returned by using <code>impl Stream&lt;Item = T&gt;</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>async_stream::stream;
<span class="kw">use </span>futures_core::stream::Stream;
<span class="kw">use </span>futures_util::pin_mut;
<span class="kw">use </span>futures_util::stream::StreamExt;
<span class="kw">fn </span>zero_to_three() -&gt; <span class="kw">impl </span>Stream&lt;Item = u32&gt; {
<span class="macro">stream! </span>{
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">3 </span>{
<span class="kw">yield </span>i;
}
}
}
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>s = zero_to_three();
<span class="macro">pin_mut!</span>(s); <span class="comment">// needed for iteration
</span><span class="kw">while let </span><span class="prelude-val">Some</span>(value) = s.next().<span class="kw">await </span>{
<span class="macro">println!</span>(<span class="string">&quot;got {}&quot;</span>, value);
}
}</code></pre></div>
<p>Streams may be implemented in terms of other streams - <code>async-stream</code> provides <code>for await</code>
syntax to assist with this:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>async_stream::stream;
<span class="kw">use </span>futures_core::stream::Stream;
<span class="kw">use </span>futures_util::pin_mut;
<span class="kw">use </span>futures_util::stream::StreamExt;
<span class="kw">fn </span>zero_to_three() -&gt; <span class="kw">impl </span>Stream&lt;Item = u32&gt; {
<span class="macro">stream! </span>{
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">3 </span>{
<span class="kw">yield </span>i;
}
}
}
<span class="kw">fn </span>double&lt;S: Stream&lt;Item = u32&gt;&gt;(input: S)
-&gt; <span class="kw">impl </span>Stream&lt;Item = u32&gt;
{
<span class="macro">stream! </span>{
<span class="kw">for await </span>value <span class="kw">in </span>input {
<span class="kw">yield </span>value * <span class="number">2</span>;
}
}
}
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span>s = double(zero_to_three());
<span class="macro">pin_mut!</span>(s); <span class="comment">// needed for iteration
</span><span class="kw">while let </span><span class="prelude-val">Some</span>(value) = s.next().<span class="kw">await </span>{
<span class="macro">println!</span>(<span class="string">&quot;got {}&quot;</span>, value);
}
}</code></pre></div>
<p>Rust try notation (<code>?</code>) can be used with the <code>try_stream!</code> macro. The <code>Item</code>
of the returned stream is <code>Result</code> with <code>Ok</code> being the value yielded and
<code>Err</code> the error type returned by <code>?</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::{TcpListener, TcpStream};
<span class="kw">use </span>async_stream::try_stream;
<span class="kw">use </span>futures_core::stream::Stream;
<span class="kw">use </span>std::io;
<span class="kw">use </span>std::net::SocketAddr;
<span class="kw">fn </span>bind_and_accept(addr: SocketAddr)
-&gt; <span class="kw">impl </span>Stream&lt;Item = io::Result&lt;TcpStream&gt;&gt;
{
<span class="macro">try_stream! </span>{
<span class="kw">let </span><span class="kw-2">mut </span>listener = TcpListener::bind(addr).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">loop </span>{
<span class="kw">let </span>(stream, addr) = listener.accept().<span class="kw">await</span><span class="question-mark">?</span>;
<span class="macro">println!</span>(<span class="string">&quot;received on {:?}&quot;</span>, addr);
<span class="kw">yield </span>stream;
}
}
}</code></pre></div>
<h2 id="implementation"><a href="#implementation">Implementation</a></h2>
<p>The <code>stream!</code> and <code>try_stream!</code> macros are implemented using proc macros.
The macro searches the syntax tree for instances of <code>yield $expr</code> and
transforms them into <code>sender.send($expr).await</code>.</p>
<p>The stream uses a lightweight sender to send values from the stream
implementation to the caller. When entering the stream, an <code>Option&lt;T&gt;</code> is
stored on the stack. A pointer to the cell is stored in a thread local and
<code>poll</code> is called on the async block. When <code>poll</code> returns.
<code>sender.send(value)</code> stores the value that cell and yields back to the
caller.</p>
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.stream.html" title="async_stream::stream macro">stream</a></div><div class="item-right docblock-short">Asynchronous stream</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.try_stream.html" title="async_stream::try_stream macro">try_stream</a></div><div class="item-right docblock-short">Asynchronous fallible stream</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="async_stream" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>