blob: a5af57398ca868f7a3407efcd619f64c3f976c2d [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="Stream utilities for Tokio."><meta name="keywords" content="rust, rustlang, rust-lang, tokio_stream"><title>tokio_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="../tokio_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="../tokio_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 tokio_stream</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.1.14</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</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">Crate <a class="mod" href="#">tokio_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/tokio_stream/lib.rs.html#1-103">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>Stream utilities for Tokio.</p>
<p>A <code>Stream</code> is an asynchronous sequence of values. It can be thought of as
an asynchronous version of the standard library’s <code>Iterator</code> trait.</p>
<p>This crate provides helpers to work with them. For examples of usage and a more in-depth
description of streams you can also refer to the <a href="https://tokio.rs/tokio/tutorial/streams">streams
tutorial</a> on the tokio website.</p>
<h2 id="iterating-over-a-stream"><a href="#iterating-over-a-stream">Iterating over a Stream</a></h2>
<p>Due to similarities with the standard library’s <code>Iterator</code> trait, some new
users may assume that they can use <code>for in</code> syntax to iterate over a
<code>Stream</code>, but this is unfortunately not possible. Instead, you can use a
<code>while let</code> loop as follows:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio_stream::{<span class="self">self </span><span class="kw">as </span>stream, StreamExt};
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() {
<span class="kw">let </span><span class="kw-2">mut </span>stream = stream::iter(<span class="macro">vec!</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>]);
<span class="kw">while let </span><span class="prelude-val">Some</span>(value) = stream.next().<span class="kw">await </span>{
<span class="macro">println!</span>(<span class="string">&quot;Got {}&quot;</span>, value);
}
}</code></pre></div>
<h2 id="returning-a-stream-from-a-function"><a href="#returning-a-stream-from-a-function">Returning a Stream from a function</a></h2>
<p>A common way to stream values from a function is to pass in the sender
half of a channel and use the receiver as the stream. This requires awaiting
both futures to ensure progress is made. Another alternative is the
<a href="https://docs.rs/async-stream">async-stream</a> crate, which contains macros that provide a <code>yield</code> keyword
and allow you to return an <code>impl Stream</code>.</p>
<h2 id="conversion-to-and-from-asyncreadasyncwrite"><a href="#conversion-to-and-from-asyncreadasyncwrite">Conversion to and from AsyncRead/AsyncWrite</a></h2>
<p>It is often desirable to convert a <code>Stream</code> into an <a href="https://docs.rs/tokio/1.0/tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a>,
especially when dealing with plaintext formats streamed over the network.
The opposite conversion from an <a href="https://docs.rs/tokio/1.0/tokio/io/trait.AsyncRead.html"><code>AsyncRead</code></a> into a <code>Stream</code> is also
another commonly required feature. To enable these conversions,
<a href="https://docs.rs/tokio-util/0.4/tokio_util/codec/index.html"><code>tokio-util</code></a> provides the <a href="https://docs.rs/tokio-util/0.4/tokio_util/io/struct.StreamReader.html"><code>StreamReader</code></a> and <a href="https://docs.rs/tokio-util/0.4/tokio_util/io/struct.ReaderStream.html"><code>ReaderStream</code></a>
types when the io feature is enabled.</p>
</div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2><div class="item-table"><div class="item-row"><div class="item-left import-item" id="reexport.Stream"><code>pub use futures_core::<a class="trait" href="../futures_core/stream/trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>;</code></div></div></div><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="wrappers/index.html" title="tokio_stream::wrappers mod">wrappers</a></div><div class="item-right docblock-short">Wrappers for Tokio types that implement <code>Stream</code>.</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.Elapsed.html" title="tokio_stream::Elapsed struct">Elapsed</a></div><div class="item-right docblock-short">Error returned by <code>Timeout</code> and <code>TimeoutRepeating</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Empty.html" title="tokio_stream::Empty struct">Empty</a></div><div class="item-right docblock-short">Stream for the <a href="fn.empty.html"><code>empty</code></a> function.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Iter.html" title="tokio_stream::Iter struct">Iter</a></div><div class="item-right docblock-short">Stream for the <a href="fn.iter.html"><code>iter</code></a> function.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Once.html" title="tokio_stream::Once struct">Once</a></div><div class="item-right docblock-short">Stream for the <a href="fn.once.html"><code>once</code></a> function.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Pending.html" title="tokio_stream::Pending struct">Pending</a></div><div class="item-right docblock-short">Stream for the <a href="fn.pending.html"><code>pending</code></a> function.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.StreamMap.html" title="tokio_stream::StreamMap struct">StreamMap</a></div><div class="item-right docblock-short">Combine many streams into one, indexing each source stream with a unique
key.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.StreamNotifyClose.html" title="tokio_stream::StreamNotifyClose struct">StreamNotifyClose</a></div><div class="item-right docblock-short">A <code>Stream</code> that wraps the values in an <code>Option</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Timeout.html" title="tokio_stream::Timeout struct">Timeout</a></div><div class="item-right docblock-short">Stream returned by the <a href="trait.StreamExt.html#method.timeout"><code>timeout</code></a> method.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.FromStream.html" title="tokio_stream::FromStream trait">FromStream</a></div><div class="item-right docblock-short">Convert from a <a href="../futures_core/stream/trait.Stream.html"><code>Stream</code></a>.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.StreamExt.html" title="tokio_stream::StreamExt trait">StreamExt</a></div><div class="item-right docblock-short">An extension trait for the <a href="../futures_core/stream/trait.Stream.html"><code>Stream</code></a> trait that provides a variety of
convenient combinator functions.</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.empty.html" title="tokio_stream::empty fn">empty</a></div><div class="item-right docblock-short">Creates a stream that yields nothing.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.iter.html" title="tokio_stream::iter fn">iter</a></div><div class="item-right docblock-short">Converts an <code>Iterator</code> into a <code>Stream</code> which is always ready
to yield the next value.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.once.html" title="tokio_stream::once fn">once</a></div><div class="item-right docblock-short">Creates a stream that emits an element exactly once.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.pending.html" title="tokio_stream::pending fn">pending</a></div><div class="item-right docblock-short">Creates a stream that is never ready</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="tokio_stream" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>