225 lines
13 KiB
HTML
225 lines
13 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 `lazy_static` crate.">
|
|||
|
<meta name="keywords" content="rust, rustlang, rust-lang, lazy_static">
|
|||
|
|
|||
|
<title>lazy_static - 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">☰</div>
|
|||
|
|
|||
|
<p class='location'>Crate lazy_static</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'lazy_static', ty: 'mod', relpath: '../'};</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'>Crate <a class="mod" href=''>lazy_static</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'>−</span>]
|
|||
|
</a>
|
|||
|
</span><a class='srclink' href='../src/lazy_static/lib.rs.html#8-223' title='goto source code'>[src]</a></span></h1>
|
|||
|
<div class='docblock'><p>A macro for declaring lazily evaluated statics.</p>
|
|||
|
<p>Using this macro, it is possible to have <code>static</code>s that require code to be
|
|||
|
executed at runtime in order to be initialized.
|
|||
|
This includes anything requiring heap allocations, like vectors or hash maps,
|
|||
|
as well as anything that requires function calls to be computed.</p>
|
|||
|
<h1 id="syntax" class="section-header"><a href="#syntax">Syntax</a></h1>
|
|||
|
<div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><pre class="rust rust-example-rendered ignore">
|
|||
|
<span class="macro">lazy_static</span><span class="macro">!</span> {
|
|||
|
[<span class="kw">pub</span>] <span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">NAME_1</span>: <span class="ident">TYPE_1</span> <span class="op">=</span> <span class="ident">EXPR_1</span>;
|
|||
|
[<span class="kw">pub</span>] <span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">NAME_2</span>: <span class="ident">TYPE_2</span> <span class="op">=</span> <span class="ident">EXPR_2</span>;
|
|||
|
...
|
|||
|
[<span class="kw">pub</span>] <span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">NAME_N</span>: <span class="ident">TYPE_N</span> <span class="op">=</span> <span class="ident">EXPR_N</span>;
|
|||
|
}</pre>
|
|||
|
<p>Attributes (including doc comments) are supported as well:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="macro">lazy_static</span><span class="macro">!</span> {
|
|||
|
<span class="doccomment">/// This is an example for using doc comment attributes</span>
|
|||
|
<span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">EXAMPLE</span>: <span class="ident">u8</span> <span class="op">=</span> <span class="number">42</span>;
|
|||
|
}</pre>
|
|||
|
<h1 id="semantics" class="section-header"><a href="#semantics">Semantics</a></h1>
|
|||
|
<p>For a given <code>static ref NAME: TYPE = EXPR;</code>, the macro generates a unique type that
|
|||
|
implements <code>Deref<TYPE></code> and stores it in a static with name <code>NAME</code>. (Attributes end up
|
|||
|
attaching to this type.)</p>
|
|||
|
<p>On first deref, <code>EXPR</code> gets evaluated and stored internally, such that all further derefs
|
|||
|
can return a reference to the same object. Note that this can lead to deadlocks
|
|||
|
if you have multiple lazy statics that depend on each other in their initialization.</p>
|
|||
|
<p>Apart from the lazy initialization, the resulting "static ref" variables
|
|||
|
have generally the same properties as regular "static" variables:</p>
|
|||
|
<ul>
|
|||
|
<li>Any type in them needs to fulfill the <code>Sync</code> trait.</li>
|
|||
|
<li>If the type has a destructor, then it will not run when the process exits.</li>
|
|||
|
</ul>
|
|||
|
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
|
|||
|
<p>Using the macro:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
|
|||
|
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">lazy_static</span>;
|
|||
|
|
|||
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">HashMap</span>;
|
|||
|
|
|||
|
<span class="macro">lazy_static</span><span class="macro">!</span> {
|
|||
|
<span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">HASHMAP</span>: <span class="ident">HashMap</span><span class="op"><</span><span class="ident">u32</span>, <span class="kw-2">&</span><span class="lifetime">'static</span> <span class="ident">str</span><span class="op">></span> <span class="op">=</span> {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">m</span> <span class="op">=</span> <span class="ident">HashMap</span>::<span class="ident">new</span>();
|
|||
|
<span class="ident">m</span>.<span class="ident">insert</span>(<span class="number">0</span>, <span class="string">"foo"</span>);
|
|||
|
<span class="ident">m</span>.<span class="ident">insert</span>(<span class="number">1</span>, <span class="string">"bar"</span>);
|
|||
|
<span class="ident">m</span>.<span class="ident">insert</span>(<span class="number">2</span>, <span class="string">"baz"</span>);
|
|||
|
<span class="ident">m</span>
|
|||
|
};
|
|||
|
<span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">COUNT</span>: <span class="ident">usize</span> <span class="op">=</span> <span class="ident">HASHMAP</span>.<span class="ident">len</span>();
|
|||
|
<span class="kw">static</span> <span class="kw-2">ref</span> <span class="ident">NUMBER</span>: <span class="ident">u32</span> <span class="op">=</span> <span class="ident">times_two</span>(<span class="number">21</span>);
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">fn</span> <span class="ident">times_two</span>(<span class="ident">n</span>: <span class="ident">u32</span>) <span class="op">-></span> <span class="ident">u32</span> { <span class="ident">n</span> <span class="op">*</span> <span class="number">2</span> }
|
|||
|
|
|||
|
<span class="kw">fn</span> <span class="ident">main</span>() {
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"The map has {} entries."</span>, <span class="kw-2">*</span><span class="ident">COUNT</span>);
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"The entry for `0` is \"{}\"."</span>, <span class="ident">HASHMAP</span>.<span class="ident">get</span>(<span class="kw-2">&</span><span class="number">0</span>).<span class="ident">unwrap</span>());
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"A expensive calculation on a static results in: {}."</span>, <span class="kw-2">*</span><span class="ident">NUMBER</span>);
|
|||
|
}</pre>
|
|||
|
<h1 id="implementation-details" class="section-header"><a href="#implementation-details">Implementation details</a></h1>
|
|||
|
<p>The <code>Deref</code> implementation uses a hidden static variable that is guarded by an atomic check on each access.</p>
|
|||
|
<h1 id="cargo-features" class="section-header"><a href="#cargo-features">Cargo features</a></h1>
|
|||
|
<p>This crate provides two cargo features:</p>
|
|||
|
<ul>
|
|||
|
<li><code>nightly</code>: This uses unstable language features only available on the nightly release channel for a more optimal implementation. In practice this currently means avoiding a heap allocation per static. This feature might get deprecated at a later point once all relevant optimizations are usable from stable.</li>
|
|||
|
<li><code>spin_no_std</code> (implies <code>nightly</code>): This allows using this crate in a no-std environment, by depending on the standalone <code>spin</code> crate.</li>
|
|||
|
</ul>
|
|||
|
<p>Both features depend on unstable language features, which means
|
|||
|
no guarantees can be made about them in regard to SemVer stability.</p>
|
|||
|
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
|
|||
|
<table>
|
|||
|
<tr class=' module-item'>
|
|||
|
<td><a class="macro" href="macro.lazy_static.html"
|
|||
|
title='macro lazy_static::lazy_static'>lazy_static</a></td>
|
|||
|
<td class='docblock-short'>
|
|||
|
|
|||
|
</td>
|
|||
|
</tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
|
|||
|
<table>
|
|||
|
<tr class=' module-item'>
|
|||
|
<td><a class="trait" href="trait.LazyStatic.html"
|
|||
|
title='trait lazy_static::LazyStatic'>LazyStatic</a></td>
|
|||
|
<td class='docblock-short'>
|
|||
|
<p>Support trait for enabling a few common operation on lazy static values.</p>
|
|||
|
|
|||
|
</td>
|
|||
|
</tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
|||
|
<table>
|
|||
|
<tr class=' module-item'>
|
|||
|
<td><a class="fn" href="fn.initialize.html"
|
|||
|
title='fn lazy_static::initialize'>initialize</a></td>
|
|||
|
<td class='docblock-short'>
|
|||
|
<p>Takes a shared reference to a lazy static and initializes
|
|||
|
it if it has not been already.</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>⏎</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 = "lazy_static";
|
|||
|
</script>
|
|||
|
<script src="../main.js"></script>
|
|||
|
<script defer src="../search-index.js"></script>
|
|||
|
</body>
|
|||
|
</html>
|