blob: 6392d5b7c08137226486b903a17161965ff9b38a [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="Abstractions for asynchronous programming."><meta name="keywords" content="rust, rustlang, rust-lang, futures"><title>futures - 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="../futures/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/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate futures</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.3.28</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="#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="#">futures</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/lib.rs.html#1-260">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>Abstractions for asynchronous programming.</p>
<p>This crate provides a number of core abstractions for writing asynchronous
code:</p>
<ul>
<li><a href="future/index.html">Futures</a> are single eventual values produced by
asynchronous computations. Some programming languages (e.g. JavaScript)
call this concept “promise”.</li>
<li><a href="stream/index.html">Streams</a> represent a series of values
produced asynchronously.</li>
<li><a href="sink/index.html">Sinks</a> provide support for asynchronous writing of
data.</li>
<li><a href="executor/index.html">Executors</a> are responsible for running asynchronous
tasks.</li>
</ul>
<p>The crate also contains abstractions for <a href="io/index.html">asynchronous I/O</a> and
<a href="channel/index.html">cross-task communication</a>.</p>
<p>Underlying all of this is the <em>task system</em>, which is a form of lightweight
threading. Large asynchronous computations are built up using futures,
streams and sinks, and then spawned as independent tasks that are run to
completion, but <em>do not block</em> the thread running them.</p>
<p>The following example describes how the task system context is built and used
within macros and keywords such as async and await!.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() {
<span class="kw">let </span>pool = ThreadPool::new().expect(<span class="string">&quot;Failed to build pool&quot;</span>);
<span class="kw">let </span>(tx, rx) = mpsc::unbounded::&lt;i32&gt;();
<span class="comment">// Create a future by an async block, where async is responsible for an
// implementation of Future. At this point no executor has been provided
// to this future, so it will not be running.
</span><span class="kw">let </span>fut_values = <span class="kw">async </span>{
<span class="comment">// Create another async block, again where the Future implementation
// is generated by async. Since this is inside of a parent async block,
// it will be provided with the executor of the parent block when the parent
// block is executed.
//
// This executor chaining is done by Future::poll whose second argument
// is a std::task::Context. This represents our executor, and the Future
// implemented by this async block can be polled using the parent async
// block&#39;s executor.
</span><span class="kw">let </span>fut_tx_result = <span class="kw">async move </span>{
(<span class="number">0</span>..<span class="number">100</span>).for_each(|v| {
tx.unbounded_send(v).expect(<span class="string">&quot;Failed to send&quot;</span>);
})
};
<span class="comment">// Use the provided thread pool to spawn the generated future
// responsible for transmission
</span>pool.spawn_ok(fut_tx_result);
<span class="kw">let </span>fut_values = rx
.map(|v| v * <span class="number">2</span>)
.collect();
<span class="comment">// Use the executor provided to this async block to wait for the
// future to complete.
</span>fut_values.<span class="kw">await
</span>};
<span class="comment">// Actually execute the above future, which will invoke Future::poll and
// subsequently chain appropriate Future::poll and methods needing executors
// to drive all futures. Eventually fut_values will be driven to completion.
</span><span class="kw">let </span>values: Vec&lt;i32&gt; = executor::block_on(fut_values);
<span class="macro">println!</span>(<span class="string">&quot;Values={:?}&quot;</span>, values);
}</code></pre></div>
<p>The majority of examples and code snippets in this crate assume that they are
inside an async block as written above.</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.Future"><code>pub use futures_core::future::<a class="trait" href="future/trait.Future.html" title="trait futures::future::Future">Future</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.TryFuture"><code>pub use futures_core::future::<a class="trait" href="future/trait.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.FutureExt"><code>pub use futures_util::future::<a class="trait" href="future/trait.FutureExt.html" title="trait futures::future::FutureExt">FutureExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.TryFutureExt"><code>pub use futures_util::future::<a class="trait" href="future/trait.TryFutureExt.html" title="trait futures::future::TryFutureExt">TryFutureExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.Stream"><code>pub use futures_core::stream::<a class="trait" href="stream/trait.Stream.html" title="trait futures::stream::Stream">Stream</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.TryStream"><code>pub use futures_core::stream::<a class="trait" href="stream/trait.TryStream.html" title="trait futures::stream::TryStream">TryStream</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.StreamExt"><code>pub use futures_util::stream::<a class="trait" href="stream/trait.StreamExt.html" title="trait futures::stream::StreamExt">StreamExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.TryStreamExt"><code>pub use futures_util::stream::<a class="trait" href="stream/trait.TryStreamExt.html" title="trait futures::stream::TryStreamExt">TryStreamExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.Sink"><code>pub use futures_sink::<a class="trait" href="sink/trait.Sink.html" title="trait futures::sink::Sink">Sink</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.SinkExt"><code>pub use futures_util::sink::<a class="trait" href="sink/trait.SinkExt.html" title="trait futures::sink::SinkExt">SinkExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncBufRead"><code>pub use futures_io::<a class="trait" href="io/trait.AsyncBufRead.html" title="trait futures::io::AsyncBufRead">AsyncBufRead</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncRead"><code>pub use futures_io::<a class="trait" href="io/trait.AsyncRead.html" title="trait futures::io::AsyncRead">AsyncRead</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncSeek"><code>pub use futures_io::<a class="trait" href="io/trait.AsyncSeek.html" title="trait futures::io::AsyncSeek">AsyncSeek</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncWrite"><code>pub use futures_io::<a class="trait" href="io/trait.AsyncWrite.html" title="trait futures::io::AsyncWrite">AsyncWrite</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncBufReadExt"><code>pub use futures_util::<a class="trait" href="io/trait.AsyncBufReadExt.html" title="trait futures::io::AsyncBufReadExt">AsyncBufReadExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncReadExt"><code>pub use futures_util::<a class="trait" href="io/trait.AsyncReadExt.html" title="trait futures::io::AsyncReadExt">AsyncReadExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncSeekExt"><code>pub use futures_util::<a class="trait" href="io/trait.AsyncSeekExt.html" title="trait futures::io::AsyncSeekExt">AsyncSeekExt</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.AsyncWriteExt"><code>pub use futures_util::<a class="trait" href="io/trait.AsyncWriteExt.html" title="trait futures::io::AsyncWriteExt">AsyncWriteExt</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="channel/index.html" title="futures::channel mod">channel</a></div><div class="item-right docblock-short">Asynchronous channels.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="executor/index.html" title="futures::executor mod">executor</a></div><div class="item-right docblock-short">Built-in executors and related tools.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="future/index.html" title="futures::future mod">future</a></div><div class="item-right docblock-short">Asynchronous values.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="io/index.html" title="futures::io mod">io</a></div><div class="item-right docblock-short">Asynchronous I/O.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="lock/index.html" title="futures::lock mod">lock</a></div><div class="item-right docblock-short">Futures-powered synchronization primitives.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="never/index.html" title="futures::never mod">never</a></div><div class="item-right docblock-short">This module contains the <code>Never</code> type.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="prelude/index.html" title="futures::prelude mod">prelude</a></div><div class="item-right docblock-short">A “prelude” for crates using the <code>futures</code> crate.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="sink/index.html" title="futures::sink mod">sink</a></div><div class="item-right docblock-short">Asynchronous sinks.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="stream/index.html" title="futures::stream mod">stream</a></div><div class="item-right docblock-short">Asynchronous streams.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="task/index.html" title="futures::task mod">task</a></div><div class="item-right docblock-short">Tools for working with tasks.</div></div></div><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.join.html" title="futures::join macro">join</a></div><div class="item-right docblock-short">Polls multiple futures simultaneously, returning a tuple
of all results once complete.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.pending.html" title="futures::pending macro">pending</a></div><div class="item-right docblock-short">A macro which yields to the event loop once.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.pin_mut.html" title="futures::pin_mut macro">pin_mut</a></div><div class="item-right docblock-short">Pins a value on the stack.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.poll.html" title="futures::poll macro">poll</a></div><div class="item-right docblock-short">A macro which returns the result of polling a future once within the
current <code>async</code> context.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.ready.html" title="futures::ready macro">ready</a></div><div class="item-right docblock-short">Extracts the successful type of a <code>Poll&lt;T&gt;</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.select.html" title="futures::select macro">select</a></div><div class="item-right docblock-short">Polls multiple futures and streams simultaneously, executing the branch
for the future that finishes first. If multiple futures are ready,
one will be pseudo-randomly selected at runtime. Futures directly
passed to <code>select!</code> must be <code>Unpin</code> and implement <code>FusedFuture</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.select_biased.html" title="futures::select_biased macro">select_biased</a></div><div class="item-right docblock-short">Polls multiple futures and streams simultaneously, executing the branch
for the future that finishes first. Unlike <a href="macro.select.html"><code>select!</code></a>, if multiple futures are ready,
one will be selected in order of declaration. Futures directly
passed to <code>select_biased!</code> must be <code>Unpin</code> and implement <code>FusedFuture</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.stream_select.html" title="futures::stream_select macro">stream_select</a></div><div class="item-right docblock-short">Combines several streams, all producing the same <code>Item</code> type, into one stream.
This is similar to <code>select_all</code> but does not require the streams to all be the same type.
It also keeps the streams inline, and does not require <code>Box&lt;dyn Stream&gt;</code>s to be allocated.
Streams passed to this macro must be <code>Unpin</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.try_join.html" title="futures::try_join macro">try_join</a></div><div class="item-right docblock-short">Polls multiple futures simultaneously, resolving to a [<code>Result</code>] containing
either a tuple of the successful outputs or an error.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="futures" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>