blob: 883c5f8d2c25e34c064e1dc0ed67d510ec806edd [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="A stream of values produced asynchronously."><meta name="keywords" content="rust, rustlang, rust-lang, Stream"><title>Stream in futures_core::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="sidebar-items.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 trait"><!--[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="../../futures_core/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="../../futures_core/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Stream</a></h2><div class="sidebar-elems"><section><h3><a href="#required-associated-types">Required Associated Types</a></h3><ul class="block"><li><a href="#associatedtype.Item">Item</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.poll_next">poll_next</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.size_hint">size_hint</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Stream-for-%26mut%20S">&amp;mut S</a></li><li><a href="#impl-Stream-for-AssertUnwindSafe%3CS%3E">AssertUnwindSafe&lt;S&gt;</a></li><li><a href="#impl-Stream-for-Box%3CS%3E">Box&lt;S&gt;</a></li><li><a href="#impl-Stream-for-Pin%3CP%3E">Pin&lt;P&gt;</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In futures_core::stream</a></h2></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">Trait <a href="../index.html">futures_core</a>::<wbr><a href="index.html">stream</a>::<wbr><a class="trait" 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/futures_core/stream.rs.html#27-98">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait Stream {
type <a href="#associatedtype.Item" class="associatedtype">Item</a>;
fn <a href="#tymethod.poll_next" class="fnname">poll_next</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self: Pin&lt;&amp;mut Self&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cx: &amp;mut Context&lt;'_&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; Poll&lt;Option&lt;Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a>&gt;&gt;;
fn <a href="#method.size_hint" class="fnname">size_hint</a>(&amp;self) -&gt; (usize, Option&lt;usize&gt;) { ... }
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A stream of values produced asynchronously.</p>
<p>If <code>Future&lt;Output = T&gt;</code> is an asynchronous version of <code>T</code>, then <code>Stream&lt;Item = T&gt;</code> is an asynchronous version of <code>Iterator&lt;Item = T&gt;</code>. A stream
represents a sequence of value-producing events that occur asynchronously to
the caller.</p>
<p>The trait is modeled after <code>Future</code>, but allows <code>poll_next</code> to be called
even after a value has been produced, yielding <code>None</code> once the stream has
been fully exhausted.</p>
</div></details><h2 id="required-associated-types" class="small-section-header">Required Associated Types<a href="#required-associated-types" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="associatedtype.Item" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#29">source</a><h4 class="code-header">type <a href="#associatedtype.Item" class="associatedtype">Item</a></h4></section></summary><div class="docblock"><p>Values yielded by the stream.</p>
</div></details></div><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.poll_next" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#66">source</a><h4 class="code-header">fn <a href="#tymethod.poll_next" class="fnname">poll_next</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self: Pin&lt;&amp;mut Self&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;cx: &amp;mut Context&lt;'_&gt;<br>) -&gt; Poll&lt;Option&lt;Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a>&gt;&gt;</h4></section></summary><div class="docblock"><p>Attempt to pull out the next value of this stream, registering the
current task for wakeup if the value is not yet available, and returning
<code>None</code> if the stream is exhausted.</p>
<h5 id="return-value"><a href="#return-value">Return value</a></h5>
<p>There are several possible return values, each indicating a distinct
stream state:</p>
<ul>
<li>
<p><code>Poll::Pending</code> means that this stream’s next value is not ready
yet. Implementations will ensure that the current task will be notified
when the next value may be ready.</p>
</li>
<li>
<p><code>Poll::Ready(Some(val))</code> means that the stream has successfully
produced a value, <code>val</code>, and may produce further values on subsequent
<code>poll_next</code> calls.</p>
</li>
<li>
<p><code>Poll::Ready(None)</code> means that the stream has terminated, and
<code>poll_next</code> should not be invoked again.</p>
</li>
</ul>
<h5 id="panics"><a href="#panics">Panics</a></h5>
<p>Once a stream has finished (returned <code>Ready(None)</code> from <code>poll_next</code>), calling its
<code>poll_next</code> method again may panic, block forever, or cause other kinds of
problems; the <code>Stream</code> trait places no requirements on the effects of
such a call. However, as the <code>poll_next</code> method is not marked <code>unsafe</code>,
Rust’s usual rules apply: calls must never cause undefined behavior
(memory corruption, incorrect use of <code>unsafe</code> functions, or the like),
regardless of the stream’s state.</p>
<p>If this is difficult to guard against then the <a href="https://docs.rs/futures/0.3/futures/stream/trait.StreamExt.html#method.fuse"><code>fuse</code></a> adapter can be used
to ensure that <code>poll_next</code> always returns <code>Ready(None)</code> in subsequent
calls.</p>
</div></details></div><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.size_hint" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#95-97">source</a><h4 class="code-header">fn <a href="#method.size_hint" class="fnname">size_hint</a>(&amp;self) -&gt; (usize, Option&lt;usize&gt;)</h4></section></summary><div class="docblock"><p>Returns the bounds on the remaining length of the stream.</p>
<p>Specifically, <code>size_hint()</code> returns a tuple where the first element
is the lower bound, and the second element is the upper bound.</p>
<p>The second half of the tuple that is returned is an [<code>Option</code>]<code>&lt;</code>[<code>usize</code>]<code>&gt;</code>.
A [<code>None</code>] here means that either there is no known upper bound, or the
upper bound is larger than [<code>usize</code>].</p>
<h5 id="implementation-notes"><a href="#implementation-notes">Implementation notes</a></h5>
<p>It is not enforced that a stream implementation yields the declared
number of elements. A buggy stream may yield less than the lower bound
or more than the upper bound of elements.</p>
<p><code>size_hint()</code> is primarily intended to be used for optimizations such as
reserving space for the elements of the stream, but must not be
trusted to e.g., omit bounds checks in unsafe code. An incorrect
implementation of <code>size_hint()</code> should not lead to memory safety
violations.</p>
<p>That said, the implementation should provide a correct estimation,
because otherwise it would be a violation of the trait’s protocol.</p>
<p>The default implementation returns <code>(0, </code>[<code>None</code>]<code>)</code> which is correct for any
stream.</p>
</div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Stream-for-%26mut%20S" class="impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#100-110">source</a><a href="#impl-Stream-for-%26mut%20S" class="anchor"></a><h3 class="code-header">impl&lt;S:&nbsp;?Sized + <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a> + Unpin&gt; <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a> for &amp;mut S</h3></section></summary><div class="impl-items"><section id="associatedtype.Item-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Item-1" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Item" class="associatedtype">Item</a> = &lt;S as <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&gt;::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a></h4></section><section id="method.poll_next" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#103-105">source</a><a href="#method.poll_next" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.poll_next" class="fnname">poll_next</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self: Pin&lt;&amp;mut Self&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;cx: &amp;mut Context&lt;'_&gt;<br>) -&gt; Poll&lt;Option&lt;Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a>&gt;&gt;</h4></section><section id="method.size_hint-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#107-109">source</a><a href="#method.size_hint-1" class="anchor"></a><h4 class="code-header">fn <a href="#method.size_hint" class="fnname">size_hint</a>(&amp;self) -&gt; (usize, Option&lt;usize&gt;)</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Stream-for-Pin%3CP%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#112-126">source</a><a href="#impl-Stream-for-Pin%3CP%3E" class="anchor"></a><h3 class="code-header">impl&lt;P&gt; <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a> for Pin&lt;P&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;P: DerefMut + Unpin,<br>&nbsp;&nbsp;&nbsp;&nbsp;P::Target: <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>,</span></h3></section></summary><div class="impl-items"><section id="associatedtype.Item-2" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Item-2" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Item" class="associatedtype">Item</a> = &lt;&lt;P as Deref&gt;::Target as <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&gt;::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a></h4></section><section id="method.poll_next-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#119-121">source</a><a href="#method.poll_next-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.poll_next" class="fnname">poll_next</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self: Pin&lt;&amp;mut Self&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;cx: &amp;mut Context&lt;'_&gt;<br>) -&gt; Poll&lt;Option&lt;Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a>&gt;&gt;</h4></section><section id="method.size_hint-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#123-125">source</a><a href="#method.size_hint-2" class="anchor"></a><h4 class="code-header">fn <a href="#method.size_hint" class="fnname">size_hint</a>(&amp;self) -&gt; (usize, Option&lt;usize&gt;)</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Stream-for-Box%3CS%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#205-215">source</a><a href="#impl-Stream-for-Box%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S:&nbsp;?Sized + <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a> + Unpin&gt; <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a> for Box&lt;S&gt;</h3></section></summary><div class="impl-items"><section id="associatedtype.Item-3" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Item-3" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Item" class="associatedtype">Item</a> = &lt;S as <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&gt;::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a></h4></section><section id="method.poll_next-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#208-210">source</a><a href="#method.poll_next-2" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.poll_next" class="fnname">poll_next</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;self: Pin&lt;&amp;mut Self&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;cx: &amp;mut Context&lt;'_&gt;<br>) -&gt; Poll&lt;Option&lt;Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a>&gt;&gt;</h4></section><section id="method.size_hint-3" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#212-214">source</a><a href="#method.size_hint-3" class="anchor"></a><h4 class="code-header">fn <a href="#method.size_hint" class="fnname">size_hint</a>(&amp;self) -&gt; (usize, Option&lt;usize&gt;)</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Stream-for-AssertUnwindSafe%3CS%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#218-228">source</a><a href="#impl-Stream-for-AssertUnwindSafe%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S:&nbsp;<a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&gt; <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a> for AssertUnwindSafe&lt;S&gt;</h3></section></summary><div class="impl-items"><section id="associatedtype.Item-4" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Item-4" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Item" class="associatedtype">Item</a> = &lt;S as <a class="trait" href="trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&gt;::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a></h4></section><section id="method.poll_next-3" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#221-223">source</a><a href="#method.poll_next-3" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.poll_next" class="fnname">poll_next</a>(self: Pin&lt;&amp;mut Self&gt;, cx: &amp;mut Context&lt;'_&gt;) -&gt; Poll&lt;Option&lt;S::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_core::stream::Stream::Item">Item</a>&gt;&gt;</h4></section><section id="method.size_hint-4" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/futures_core/stream.rs.html#225-227">source</a><a href="#method.size_hint-4" class="anchor"></a><h4 class="code-header">fn <a href="#method.size_hint" class="fnname">size_hint</a>(&amp;self) -&gt; (usize, Option&lt;usize&gt;)</h4></section></div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"></div><script src="../../implementors/futures_core/stream/trait.Stream.js" data-ignore-extern-crates="core,alloc" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="futures_core" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>