mentat/tokio_executor/trait.Executor.html
2018-08-22 17:04:13 +00:00

230 lines
No EOL
18 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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="API documentation for the Rust `Executor` trait in crate `tokio_executor`.">
<meta name="keywords" content="rust, rustlang, rust-lang, Executor">
<title>tokio_executor::Executor - Rust</title>
<link rel="stylesheet" type="text/css" href="../normalize.css">
<link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
<link rel="stylesheet" type="text/css" href="../dark.css">
<link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
<script src="../storage.js"></script>
</head>
<body class="rustdoc trait">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<div class="sidebar-menu">&#9776;</div>
<p class='location'>Trait Executor</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.spawn">spawn</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.status">status</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-Executor">Box&lt;E&gt;</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='index.html'>tokio_executor</a></p><script>window.sidebarCurrent = {name: 'Executor', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div>
</nav>
<div class="theme-picker">
<button id="theme-picker" aria-label="Pick another theme!">
<img src="../brush.svg" width="18" alt="Pick another theme!">
</button>
<div id="theme-choices"></div>
</div>
<script src="../theme.js"></script>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Trait <a href='index.html'>tokio_executor</a>::<wbr><a class="trait" href=''>Executor</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../src/tokio_executor/lib.rs.html#111-185' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'>pub trait Executor {
fn <a href='#tymethod.spawn' class='fnname'>spawn</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;<a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a>&lt;Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Error = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../tokio_executor/struct.SpawnError.html" title="struct tokio_executor::SpawnError">SpawnError</a>&gt;;
fn <a href='#method.status' class='fnname'>status</a>(&amp;self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../tokio_executor/struct.SpawnError.html" title="struct tokio_executor::SpawnError">SpawnError</a>&gt; { ... }
}</pre><div class='docblock'><p>A value that executes futures.</p>
<p>The <a href="#tymethod.spawn"><code>spawn</code></a> function is used to submit a future to an executor. Once
submitted, the executor takes ownership of the future and becomes
responsible for driving the future to completion.</p>
<p>The strategy employed by the executor to handle the future is less defined
and is left up to the <code>Executor</code> implementation. The <code>Executor</code> instance is
expected to call <a href="https://docs.rs/futures/0.1/futures/future/trait.Future.html#tymethod.poll"><code>poll</code></a> on the future once it has been notified, however
the &quot;when&quot; and &quot;how&quot; can vary greatly.</p>
<p>For example, the executor might be a thread pool, in which case a set of
threads have already been spawned up and the future is inserted into a
queue. A thread will acquire the future and poll it.</p>
<p>The <code>Executor</code> trait is only for futures that <strong>are</strong> <code>Send</code>. These are most
common. There currently is no trait that describes executors that operate
entirely on the current thread (i.e., are able to spawn futures that are not
<code>Send</code>). Note that single threaded executors can still implement <code>Executor</code>,
but only futures that are <code>Send</code> can be spawned via the trait.</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>The <a href="#tymethod.spawn"><code>spawn</code></a> function returns <code>Result</code> with an error type of <code>SpawnError</code>.
This error type represents the reason that the executor was unable to spawn
the future. The two current represented scenarios are:</p>
<ul>
<li>An executor being at capacity or full. As such, the executor is not able
to accept a new future. This error state is expected to be transient.</li>
<li>An executor has been shutdown and can no longer accept new futures. This
error state is expected to be permanent.</li>
</ul>
<p>If a caller encounters an at capacity error, the caller should try to shed
load. This can be as simple as dropping the future that was spawned.</p>
<p>If the caller encounters a shutdown error, the caller should attempt to
gracefully shutdown.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::<span class="ident">lazy</span>;
<span class="ident">my_executor</span>.<span class="ident">spawn</span>(<span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;running on the executor&quot;</span>);
<span class="prelude-val">Ok</span>(())
}))).<span class="ident">unwrap</span>();</pre>
</div>
<h2 id='required-methods' class='small-section-header'>
Required Methods<a href='#required-methods' class='anchor'></a>
</h2>
<div class='methods'>
<h3 id='tymethod.spawn' class='method'><span id='spawn.v' class='invisible'><code>fn <a href='#tymethod.spawn' class='fnname'>spawn</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;<a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a>&lt;Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Error = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>&gt;<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../tokio_executor/struct.SpawnError.html" title="struct tokio_executor::SpawnError">SpawnError</a>&gt;</code></span></h3><div class='docblock'><p>Spawns a future object to run on this executor.</p>
<p><code>future</code> is passed to the executor, which will begin running it. The
future may run on the current thread or another thread at the discretion
of the <code>Executor</code> implementation.</p>
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
<p>Implementors are encouraged to avoid panics. However, a panic is
permitted and the caller should check the implementation specific
documentation for more details on possible panics.</p>
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::<span class="ident">lazy</span>;
<span class="ident">my_executor</span>.<span class="ident">spawn</span>(<span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;running on the executor&quot;</span>);
<span class="prelude-val">Ok</span>(())
}))).<span class="ident">unwrap</span>();</pre>
</div></div>
<h2 id='provided-methods' class='small-section-header'>
Provided Methods<a href='#provided-methods' class='anchor'></a>
</h2>
<div class='methods'>
<h3 id='method.status' class='method'><span id='status.v' class='invisible'><code>fn <a href='#method.status' class='fnname'>status</a>(&amp;self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../tokio_executor/struct.SpawnError.html" title="struct tokio_executor::SpawnError">SpawnError</a>&gt;</code></span></h3><div class='docblock'><p>Provides a best effort <strong>hint</strong> to whether or not <code>spawn</code> will succeed.</p>
<p>This function may return both false positives <strong>and</strong> false negatives.
If <code>status</code> returns <code>Ok</code>, then a call to <code>spawn</code> will <em>probably</em>
succeed, but may fail. If <code>status</code> returns <code>Err</code>, a call to <code>spawn</code> will
<em>probably</em> fail, but may succeed.</p>
<p>This allows a caller to avoid creating the task if the call to <code>spawn</code>
has a high likelihood of failing.</p>
<h1 id="panics-1" class="section-header"><a href="#panics-1">Panics</a></h1>
<p>This function must not panic. Implementors must ensure that panics do
not happen.</p>
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::<span class="ident">lazy</span>;
<span class="kw">if</span> <span class="ident">my_executor</span>.<span class="ident">status</span>().<span class="ident">is_ok</span>() {
<span class="ident">my_executor</span>.<span class="ident">spawn</span>(<span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;running on the executor&quot;</span>);
<span class="prelude-val">Ok</span>(())
}))).<span class="ident">unwrap</span>();
} <span class="kw">else</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;the executor is not in a good state&quot;</span>);
}</pre>
</div></div>
<h2 id='foreign-impls' class='small-section-header'>
Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a>
</h2>
<h3 id='impl-Executor' class='impl'><span class='in-band'><code>impl&lt;E:&nbsp;<a class="trait" href="../tokio_executor/trait.Executor.html" title="trait tokio_executor::Executor">Executor</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <a class="trait" href="../tokio_executor/trait.Executor.html" title="trait tokio_executor::Executor">Executor</a> for <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;E&gt;</code><a href='#impl-Executor' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../src/tokio_executor/lib.rs.html#187-204' title='goto source code'>[src]</a></span></h3>
<span class='docblock autohide'><div class='impl-items'><h4 id='method.spawn' class="method"><span id='spawn.v-1' class='invisible'><code>fn <a href='#method.spawn' class='fnname'>spawn</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;<a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a>&lt;Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Error = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>&gt;<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../tokio_executor/struct.SpawnError.html" title="struct tokio_executor::SpawnError">SpawnError</a>&gt;</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../src/tokio_executor/lib.rs.html#188-192' title='goto source code'>[src]</a></span></h4>
<h4 id='method.status-1' class="method"><span id='status.v-1' class='invisible'><code>fn <a href='#method.status' class='fnname'>status</a>(&amp;self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../tokio_executor/struct.SpawnError.html" title="struct tokio_executor::SpawnError">SpawnError</a>&gt;</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../src/tokio_executor/lib.rs.html#201-203' title='goto source code'>[src]</a></span></h4>
</div></span>
<h2 id='implementors' class='small-section-header'>
Implementors<a href='#implementors' class='anchor'></a>
</h2>
<ul class='item-list' id='implementors-list'>
<li><div class='out-of-band'><a class='srclink' href='../src/tokio_executor/global.rs.html#75-95' title='goto source code'>[src]</a></div><code>impl Executor for <a class="struct" href="../tokio_executor/struct.DefaultExecutor.html" title="struct tokio_executor::DefaultExecutor">DefaultExecutor</a></code></li>
</ul><script type="text/javascript" async
src="../implementors/tokio_executor/trait.Executor.js">
</script></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../";
window.currentCrate = "tokio_executor";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>