blob: c9dac4c68e6f2ad03028f066acbd88d38dbf5d49 [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="Due to the `Stream` trait’s inclusion in `std` landing later than Tokio’s 1.0 release, most of the Tokio stream utilities have been moved into the `tokio-stream` crate."><meta name="keywords" content="rust, rustlang, rust-lang, 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="../../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 stream</a></h2><div class="sidebar-elems"></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 class="mod" href="#">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/lib.rs.html#568">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>Due to the <code>Stream</code> trait’s inclusion in <code>std</code> landing later than Tokio’s 1.0
release, most of the Tokio stream utilities have been moved into the <a href="https://docs.rs/tokio-stream"><code>tokio-stream</code></a>
crate.</p>
<h2 id="why-was-stream-not-included-in-tokio-10"><a href="#why-was-stream-not-included-in-tokio-10">Why was <code>Stream</code> not included in Tokio 1.0?</a></h2>
<p>Originally, we had planned to ship Tokio 1.0 with a stable <code>Stream</code> type
but unfortunately the <a href="https://github.com/rust-lang/rfcs/pull/2996">RFC</a> had not been merged in time for <code>Stream</code> to
reach <code>std</code> on a stable compiler in time for the 1.0 release of Tokio. For
this reason, the team has decided to move all <code>Stream</code> based utilities to
the <a href="https://docs.rs/tokio-stream"><code>tokio-stream</code></a> crate. While this is not ideal, once <code>Stream</code> has made
it into the standard library and the MSRV period has passed, we will implement
stream for our different types.</p>
<p>While this may seem unfortunate, not all is lost as you can get much of the
<code>Stream</code> support with <code>async/await</code> and <code>while let</code> loops. It is also possible
to create a <code>impl Stream</code> from <code>async fn</code> using the <a href="https://docs.rs/async-stream"><code>async-stream</code></a> crate.</p>
<h2 id="example"><a href="#example">Example</a></h2>
<p>Convert a <a href="../sync/mpsc/struct.Receiver.html" title="sync::mpsc::Receiver"><code>sync::mpsc::Receiver</code></a> to an <code>impl Stream</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::sync::mpsc;
<span class="kw">let </span>(tx, <span class="kw-2">mut </span>rx) = mpsc::channel::&lt;usize&gt;(<span class="number">16</span>);
<span class="kw">let </span>stream = <span class="macro">async_stream::stream! </span>{
<span class="kw">while let </span><span class="prelude-val">Some</span>(item) = rx.recv().<span class="kw">await </span>{
<span class="kw">yield </span>item;
}
};</code></pre></div>
</div></details></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>