mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-14 03:58:23 +00:00
69 lines
9.3 KiB
HTML
69 lines
9.3 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="A macro for declaring lazily evaluated statics."><title>lazy_static - 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="lazy_static" 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="../lazy_static/index.html">lazy_static</a><span class="version">1.4.0</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="#macros">Macros</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</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="../lazy_static/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="#">lazy_static</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/lazy_static/lib.rs.html#8-215">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>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>
|
||
<h2 id="syntax"><a href="#syntax">Syntax</a></h2>
|
||
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="macro">lazy_static! </span>{
|
||
[<span class="kw">pub</span>] <span class="kw">static </span><span class="kw-2">ref </span>NAME_1: TYPE_1 = EXPR_1;
|
||
[<span class="kw">pub</span>] <span class="kw">static </span><span class="kw-2">ref </span>NAME_2: TYPE_2 = EXPR_2;
|
||
...
|
||
[<span class="kw">pub</span>] <span class="kw">static </span><span class="kw-2">ref </span>NAME_N: TYPE_N = EXPR_N;
|
||
}</code></pre></div>
|
||
<p>Attributes (including doc comments) are supported as well:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">lazy_static! </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>EXAMPLE: u8 = <span class="number">42</span>;
|
||
}</code></pre></div>
|
||
<h2 id="semantics"><a href="#semantics">Semantics</a></h2>
|
||
<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>
|
||
<h2 id="example"><a href="#example">Example</a></h2>
|
||
<p>Using the macro:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[macro_use]
|
||
</span><span class="kw">extern crate </span>lazy_static;
|
||
|
||
<span class="kw">use </span>std::collections::HashMap;
|
||
|
||
<span class="macro">lazy_static! </span>{
|
||
<span class="kw">static </span><span class="kw-2">ref </span>HASHMAP: HashMap<u32, <span class="kw-2">&</span><span class="lifetime">'static </span>str> = {
|
||
<span class="kw">let </span><span class="kw-2">mut </span>m = HashMap::new();
|
||
m.insert(<span class="number">0</span>, <span class="string">"foo"</span>);
|
||
m.insert(<span class="number">1</span>, <span class="string">"bar"</span>);
|
||
m.insert(<span class="number">2</span>, <span class="string">"baz"</span>);
|
||
m
|
||
};
|
||
<span class="kw">static </span><span class="kw-2">ref </span>COUNT: usize = HASHMAP.len();
|
||
<span class="kw">static </span><span class="kw-2">ref </span>NUMBER: u32 = times_two(<span class="number">21</span>);
|
||
}
|
||
|
||
<span class="kw">fn </span>times_two(n: u32) -> u32 { n * <span class="number">2 </span>}
|
||
|
||
<span class="kw">fn </span>main() {
|
||
<span class="macro">println!</span>(<span class="string">"The map has {} entries."</span>, <span class="kw-2">*</span>COUNT);
|
||
<span class="macro">println!</span>(<span class="string">"The entry for `0` is \"{}\"."</span>, HASHMAP.get(<span class="kw-2">&</span><span class="number">0</span>).unwrap());
|
||
<span class="macro">println!</span>(<span class="string">"A expensive calculation on a static results in: {}."</span>, <span class="kw-2">*</span>NUMBER);
|
||
}</code></pre></div>
|
||
<h2 id="implementation-details"><a href="#implementation-details">Implementation details</a></h2>
|
||
<p>The <code>Deref</code> implementation uses a hidden static variable that is guarded by an atomic check on each access.</p>
|
||
<h2 id="cargo-features"><a href="#cargo-features">Cargo features</a></h2>
|
||
<p>This crate provides one cargo feature:</p>
|
||
<ul>
|
||
<li><code>spin_no_std</code>: This allows using this crate in a no-std environment, by depending on the standalone <code>spin</code> crate.</li>
|
||
</ul>
|
||
</div></details><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.lazy_static.html" title="macro lazy_static::lazy_static">lazy_static</a></div></li></ul><h2 id="traits" class="section-header"><a href="#traits">Traits</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.LazyStatic.html" title="trait lazy_static::LazyStatic">LazyStatic</a></div><div class="desc docblock-short">Support trait for enabling a few common operation on lazy static values.</div></li></ul><h2 id="functions" class="section-header"><a href="#functions">Functions</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.initialize.html" title="fn lazy_static::initialize">initialize</a></div><div class="desc docblock-short">Takes a shared reference to a lazy static and initializes
|
||
it if it has not been already.</div></li></ul></section></div></main></body></html> |