mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-01-25 12:44:07 +01:00
83 lines
17 KiB
HTML
83 lines
17 KiB
HTML
<!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."><title>futures - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="futures" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.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">☰</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../futures/index.html">futures</a><span class="version">0.3.28</span></h2></div><div class="sidebar-elems"><ul class="block">
|
||
<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><div class="sidebar-resizer"></div>
|
||
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../futures/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">futures</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/futures/lib.rs.html#1-260">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="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" title="mod futures::future">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" title="mod futures::stream">Streams</a> represent a series of values
|
||
produced asynchronously.</li>
|
||
<li><a href="sink/index.html" title="mod futures::sink">Sinks</a> provide support for asynchronous writing of
|
||
data.</li>
|
||
<li><a href="executor/index.html" title="mod futures::executor">Executors</a> are responsible for running asynchronous
|
||
tasks.</li>
|
||
</ul>
|
||
<p>The crate also contains abstractions for <a href="io/index.html" title="mod futures::io">asynchronous I/O</a> and
|
||
<a href="channel/index.html" title="mod futures::channel">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">"Failed to build pool"</span>);
|
||
<span class="kw">let </span>(tx, rx) = mpsc::unbounded::<i32>();
|
||
|
||
<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'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">"Failed to send"</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<i32> = executor::block_on(fut_values);
|
||
|
||
<span class="macro">println!</span>(<span class="string">"Values={:?}"</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="section-header"><a href="#reexports">Re-exports</a></h2><ul class="item-table"><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li><li><div class="item-name" 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></li></ul><h2 id="modules" class="section-header"><a href="#modules">Modules</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="channel/index.html" title="mod futures::channel">channel</a></div><div class="desc docblock-short">Asynchronous channels.</div></li><li><div class="item-name"><a class="mod" href="executor/index.html" title="mod futures::executor">executor</a></div><div class="desc docblock-short">Built-in executors and related tools.</div></li><li><div class="item-name"><a class="mod" href="future/index.html" title="mod futures::future">future</a></div><div class="desc docblock-short">Asynchronous values.</div></li><li><div class="item-name"><a class="mod" href="io/index.html" title="mod futures::io">io</a></div><div class="desc docblock-short">Asynchronous I/O.</div></li><li><div class="item-name"><a class="mod" href="lock/index.html" title="mod futures::lock">lock</a></div><div class="desc docblock-short">Futures-powered synchronization primitives.</div></li><li><div class="item-name"><a class="mod" href="never/index.html" title="mod futures::never">never</a></div><div class="desc docblock-short">This module contains the <code>Never</code> type.</div></li><li><div class="item-name"><a class="mod" href="prelude/index.html" title="mod futures::prelude">prelude</a></div><div class="desc docblock-short">A “prelude” for crates using the <code>futures</code> crate.</div></li><li><div class="item-name"><a class="mod" href="sink/index.html" title="mod futures::sink">sink</a></div><div class="desc docblock-short">Asynchronous sinks.</div></li><li><div class="item-name"><a class="mod" href="stream/index.html" title="mod futures::stream">stream</a></div><div class="desc docblock-short">Asynchronous streams.</div></li><li><div class="item-name"><a class="mod" href="task/index.html" title="mod futures::task">task</a></div><div class="desc docblock-short">Tools for working with tasks.</div></li></ul><h2 id="macros" class="section-header"><a href="#macros">Macros</a></h2><ul class="item-table"><li><div class="item-name"><a class="macro" href="macro.join.html" title="macro futures::join">join</a></div><div class="desc docblock-short">Polls multiple futures simultaneously, returning a tuple
|
||
of all results once complete.</div></li><li><div class="item-name"><a class="macro" href="macro.pending.html" title="macro futures::pending">pending</a></div><div class="desc docblock-short">A macro which yields to the event loop once.</div></li><li><div class="item-name"><a class="macro" href="macro.pin_mut.html" title="macro futures::pin_mut">pin_mut</a></div><div class="desc docblock-short">Pins a value on the stack.</div></li><li><div class="item-name"><a class="macro" href="macro.poll.html" title="macro futures::poll">poll</a></div><div class="desc docblock-short">A macro which returns the result of polling a future once within the
|
||
current <code>async</code> context.</div></li><li><div class="item-name"><a class="macro" href="macro.ready.html" title="macro futures::ready">ready</a></div><div class="desc docblock-short">Extracts the successful type of a <code>Poll<T></code>.</div></li><li><div class="item-name"><a class="macro" href="macro.select.html" title="macro futures::select">select</a></div><div class="desc 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></li><li><div class="item-name"><a class="macro" href="macro.select_biased.html" title="macro futures::select_biased">select_biased</a></div><div class="desc 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></li><li><div class="item-name"><a class="macro" href="macro.stream_select.html" title="macro futures::stream_select">stream_select</a></div><div class="desc 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<dyn Stream></code>s to be allocated.
|
||
Streams passed to this macro must be <code>Unpin</code>.</div></li><li><div class="item-name"><a class="macro" href="macro.try_join.html" title="macro futures::try_join">try_join</a></div><div class="desc docblock-short">Polls multiple futures simultaneously, resolving to a <a href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result"><code>Result</code></a> containing
|
||
either a tuple of the successful outputs or an error.</div></li></ul></section></div></main></body></html> |