mentat/tokio/timer/index.html
2018-08-22 17:04:13 +00:00

234 lines
No EOL
11 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 `timer` mod in crate `tokio`.">
<meta name="keywords" content="rust, rustlang, rust-lang, timer">
<title>tokio::timer - 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 mod">
<!--[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'>Module timer</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li></ul></div><p class='location'><a href='../index.html'>tokio</a></p><script>window.sidebarCurrent = {name: 'timer', ty: 'mod', 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'>Module <a href='../index.html'>tokio</a>::<wbr><a class="mod" href=''>timer</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/timer.rs.html#1-86' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>Utilities for tracking time.</p>
<p>This module provides a number of types for executing code after a set period
of time.</p>
<ul>
<li>
<p>[<code>Delay</code>][Delay] is a future that does no work and completes at a specific <code>Instant</code>
in time.</p>
</li>
<li>
<p>[<code>Interval</code>][Interval] is a stream yielding a value at a fixed period. It
is initialized with a <code>Duration</code> and repeatedly yields each time the
duration elapses.</p>
</li>
<li>
<p>[<code>Deadline</code>][Deadline] wraps a future, requiring that it completes before
a specified <code>Instant</code> in time. If the future does not complete in time,
then it is canceled and an error is returned.</p>
</li>
</ul>
<p>These types are sufficient for handling a large number of scenarios
involving time.</p>
<p>These types must be used from within the context of the
<a href="../runtime/struct.Runtime.html"><code>Runtime</code></a> or a timer context must be setup explicitly. See the
<a href="https://docs.rs/tokio-timer"><code>tokio-timer</code></a> crate for more details on how to setup a timer
context.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Wait 100ms and print &quot;Hello World!&quot;</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">timer</span>::<span class="ident">Delay</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::{<span class="ident">Duration</span>, <span class="ident">Instant</span>};
<span class="kw">let</span> <span class="ident">when</span> <span class="op">=</span> <span class="ident">Instant</span>::<span class="ident">now</span>() <span class="op">+</span> <span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">100</span>);
<span class="ident">tokio</span>::<span class="ident">run</span>({
<span class="ident">Delay</span>::<span class="ident">new</span>(<span class="ident">when</span>)
.<span class="ident">map_err</span>(<span class="op">|</span><span class="ident">e</span><span class="op">|</span> <span class="macro">panic</span><span class="macro">!</span>(<span class="string">&quot;timer failed; err={:?}&quot;</span>, <span class="ident">e</span>))
.<span class="ident">and_then</span>(<span class="op">|</span>_<span class="op">|</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Hello world!&quot;</span>);
<span class="prelude-val">Ok</span>(())
})
})</pre>
<p>Require that an operation takes no more than 300ms. Note that this uses the
<a href="../util/trait.FutureExt.html#method.deadline"><code>deadline</code></a> function on the <a href="../util/trait.FutureExt.html#method.deadline"><code>FutureExt</code></a> trait. This trait is
included in the prelude.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::{<span class="ident">Duration</span>, <span class="ident">Instant</span>};
<span class="kw">fn</span> <span class="ident">long_op</span>() <span class="op">-&gt;</span> <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">Future</span><span class="op">&lt;</span><span class="ident">Item</span> <span class="op">=</span> (), <span class="ident">Error</span> <span class="op">=</span> ()<span class="op">&gt;</span> <span class="op">+</span> <span class="ident">Send</span><span class="op">&gt;</span> {
<span class="comment">// ...</span>
}
<span class="kw">let</span> <span class="ident">when</span> <span class="op">=</span> <span class="ident">Instant</span>::<span class="ident">now</span>() <span class="op">+</span> <span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">300</span>);
<span class="ident">tokio</span>::<span class="ident">run</span>({
<span class="ident">long_op</span>()
.<span class="ident">deadline</span>(<span class="ident">when</span>)
.<span class="ident">map_err</span>(<span class="op">|</span><span class="ident">e</span><span class="op">|</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;operation timed out&quot;</span>);
})
})</pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.Deadline.html"
title='struct tokio::timer::Deadline'>Deadline</a></td>
<td class='docblock-short'>
<p>Allows a given <code>Future</code> to execute until the specified deadline.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.DeadlineError.html"
title='struct tokio::timer::DeadlineError'>DeadlineError</a></td>
<td class='docblock-short'>
<p>Error returned by <code>Deadline</code> future.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Delay.html"
title='struct tokio::timer::Delay'>Delay</a></td>
<td class='docblock-short'>
<p>A future that completes at a specified instant in time.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Error.html"
title='struct tokio::timer::Error'>Error</a></td>
<td class='docblock-short'>
<p>Errors encountered by the timer implementation.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Interval.html"
title='struct tokio::timer::Interval'>Interval</a></td>
<td class='docblock-short'>
<p>A stream representing notifications at fixed interval</p>
</td>
</tr></table></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";
</script>
<script src="../../main.js"></script>
<script defer src="../../search-index.js"></script>
</body>
</html>