88 lines
19 KiB
HTML
88 lines
19 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="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="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></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">☰</div><a href='../tokio_executor/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><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-for-Box%3CE%3E">Box<E></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"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><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'>−</span>]</a></span><a class='srclink' href='../src/tokio_executor/executor.rs.html#67-138' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='index.html'>tokio_executor</a>::<wbr><a class="trait" href=''>Executor</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait Executor {
|
||
fn <a href='#tymethod.spawn' class='fnname'>spawn</a>(<br> &mut self, <br> future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a><dyn <a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a><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>> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>>;
|
||
|
||
fn <a href='#method.status' class='fnname'>status</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>> { ... }
|
||
}</pre></div><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 "when" and "how" 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>
|
||
<p>This trait is primarily intended to implemented by executors and used to
|
||
back <code>tokio::spawn</code>. Libraries and applications <strong>may</strong> use this trait to
|
||
bound generics, but doing so will limit usage to futures that implement
|
||
<code>Send</code>. Instead, libraries and applications are recommended to use
|
||
<a href="../trait.TypedExecutor.html"><code>TypedExecutor</code></a> as a bound.</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>
|
||
<div class="example-wrap"><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="op">|</span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"running on the executor"</span>);
|
||
<span class="prelude-val">Ok</span>(())
|
||
}))).<span class="ident">unwrap</span>();</pre></div>
|
||
</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'><code id='spawn.v'>fn <a href='#tymethod.spawn' class='fnname'>spawn</a>(<br> &mut self, <br> future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a><dyn <a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a><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>> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>></code></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>Implementations are encouraged to avoid panics. However, panics are
|
||
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>
|
||
<div class="example-wrap"><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="op">|</span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"running on the executor"</span>);
|
||
<span class="prelude-val">Ok</span>(())
|
||
}))).<span class="ident">unwrap</span>();</pre></div>
|
||
</div></div><span class='loading-content'>Loading content...</span>
|
||
<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'><code id='status.v'>fn <a href='#method.status' class='fnname'>status</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>></code></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. Implementers must ensure that panics do
|
||
not happen.</p>
|
||
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
|
||
<div class="example-wrap"><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="op">|</span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"running on the executor"</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">"the executor is not in a good state"</span>);
|
||
}</pre></div>
|
||
</div></div><span class='loading-content'>Loading content...</span>
|
||
<h2 id='foreign-impls' class='small-section-header'>Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a></h2><h3 id='impl-Executor-for-Box%3CE%3E' class='impl'><code class='in-band'>impl<E: <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>> <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><E></code><a href='#impl-Executor-for-Box%3CE%3E' class='anchor'></a><a class='srclink' href='../src/tokio_executor/executor.rs.html#140-151' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.spawn' class="method hidden"><code id='spawn.v-1'>fn <a href='#method.spawn' class='fnname'>spawn</a>(<br> &mut self, <br> future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a><dyn <a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a><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>> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>></code><a class='srclink' href='../src/tokio_executor/executor.rs.html#141-146' title='goto source code'>[src]</a></h4><h4 id='method.status-1' class="method hidden"><code id='status.v-1'>fn <a href='#method.status' class='fnname'>status</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>></code><a class='srclink' href='../src/tokio_executor/executor.rs.html#148-150' title='goto source code'>[src]</a></h4></div><span class='loading-content'>Loading content...</span>
|
||
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'><h3 id='impl-Executor' class='impl'><code class='in-band'>impl Executor for <a class="struct" href="../tokio_executor/struct.DefaultExecutor.html" title="struct tokio_executor::DefaultExecutor">DefaultExecutor</a></code><a href='#impl-Executor' class='anchor'></a><a class='srclink' href='../src/tokio_executor/global.rs.html#79-92' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.spawn-1' class="method hidden"><code id='spawn.v-2'>fn <a href='#method.spawn-1' class='fnname'>spawn</a>(<br> &mut self, <br> future: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a><dyn <a class="trait" href="../futures/future/trait.Future.html" title="trait futures::future::Future">Future</a><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>> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>></code><a class='srclink' href='../src/tokio_executor/global.rs.html#80-86' title='goto source code'>[src]</a></h4><h4 id='method.status-2' class="method hidden"><code id='status.v-2'>fn <a href='#method.status-2' class='fnname'>status</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<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>></code><a class='srclink' href='../src/tokio_executor/global.rs.html#88-91' title='goto source code'>[src]</a></h4></div></div><span class='loading-content'>Loading content...</span><script type="text/javascript" src="../implementors/tokio_executor/trait.Executor.js" async></script></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "tokio_executor";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |