205 lines
No EOL
11 KiB
HTML
205 lines
No EOL
11 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 `LocalKey` struct in crate `bitflags`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, LocalKey">
|
||
|
||
<title>bitflags::__core::thread::LocalKey - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../../../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../../../main.css">
|
||
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
|
||
<p class='location'><a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a href='index.html'>thread</a></p><script>window.sidebarCurrent = {name: 'LocalKey', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<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 struct">
|
||
<h1 class='fqn'><span class='in-band'>Struct <a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a href='index.html'>thread</a>::<wbr><a class='struct' href=''>LocalKey</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 id='src-788' class='srclink' href='https://doc.rust-lang.org/nightly/std/thread/local/struct.LocalKey.html?gotosrc=788' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust struct'>pub struct LocalKey<T> <span class='where'>where T: 'static</span> {
|
||
// some fields omitted
|
||
}</pre><span class="since">1.0.0</span><div class='docblock'><p>A thread local storage key which owns its contents.</p>
|
||
|
||
<p>This key uses the fastest possible implementation available to it for the
|
||
target platform. It is instantiated with the <code>thread_local!</code> macro and the
|
||
primary method is the <code>with</code> method.</p>
|
||
|
||
<p>The <code>with</code> method yields a reference to the contained value which cannot be
|
||
sent across threads or escape the given closure.</p>
|
||
|
||
<h1 id='initialization-and-destruction' class='section-header'><a href='#initialization-and-destruction'>Initialization and Destruction</a></h1>
|
||
<p>Initialization is dynamically performed on the first call to <code>with()</code>
|
||
within a thread, and values support destructors which will be run when a
|
||
thread exits.</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'>std</span>::<span class='ident'>cell</span>::<span class='ident'>RefCell</span>;
|
||
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>thread</span>;
|
||
|
||
<span class='macro'>thread_local</span><span class='macro'>!</span>(<span class='kw'>static</span> <span class='ident'>FOO</span>: <span class='ident'>RefCell</span><span class='op'><</span><span class='ident'>u32</span><span class='op'>></span> <span class='op'>=</span> <span class='ident'>RefCell</span>::<span class='ident'>new</span>(<span class='number'>1</span>));
|
||
|
||
<span class='ident'>FOO</span>.<span class='ident'>with</span>(<span class='op'>|</span><span class='ident'>f</span><span class='op'>|</span> {
|
||
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='op'>*</span><span class='ident'>f</span>.<span class='ident'>borrow</span>(), <span class='number'>1</span>);
|
||
<span class='op'>*</span><span class='ident'>f</span>.<span class='ident'>borrow_mut</span>() <span class='op'>=</span> <span class='number'>2</span>;
|
||
});
|
||
|
||
<span class='comment'>// each thread starts out with the initial value of 1</span>
|
||
<span class='ident'>thread</span>::<span class='ident'>spawn</span>(<span class='kw'>move</span><span class='op'>||</span> {
|
||
<span class='ident'>FOO</span>.<span class='ident'>with</span>(<span class='op'>|</span><span class='ident'>f</span><span class='op'>|</span> {
|
||
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='op'>*</span><span class='ident'>f</span>.<span class='ident'>borrow</span>(), <span class='number'>1</span>);
|
||
<span class='op'>*</span><span class='ident'>f</span>.<span class='ident'>borrow_mut</span>() <span class='op'>=</span> <span class='number'>3</span>;
|
||
});
|
||
});
|
||
|
||
<span class='comment'>// we retain our original value of 2 despite the child thread</span>
|
||
<span class='ident'>FOO</span>.<span class='ident'>with</span>(<span class='op'>|</span><span class='ident'>f</span><span class='op'>|</span> {
|
||
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='op'>*</span><span class='ident'>f</span>.<span class='ident'>borrow</span>(), <span class='number'>2</span>);
|
||
});</pre>
|
||
|
||
<h1 id='platform-specific-behavior' class='section-header'><a href='#platform-specific-behavior'>Platform-specific behavior</a></h1>
|
||
<p>Note that a "best effort" is made to ensure that destructors for types
|
||
stored in thread local storage are run, but not all platforms can guarantee
|
||
that destructors will be run for all types in thread local storage. For
|
||
example, there are a number of known caveats where destructors are not run:</p>
|
||
|
||
<ol>
|
||
<li>On Unix systems when pthread-based TLS is being used, destructors will
|
||
not be run for TLS values on the main thread when it exits. Note that the
|
||
application will exit immediately after the main thread exits as well.</li>
|
||
<li>On all platforms it's possible for TLS to re-initialize other TLS slots
|
||
during destruction. Some platforms ensure that this cannot happen
|
||
infinitely by preventing re-initialization of any slot that has been
|
||
destroyed, but not all platforms have this guard. Those platforms that do
|
||
not guard typically have a synthetic limit after which point no more
|
||
destructors are run.</li>
|
||
<li>On OSX, initializing TLS during destruction of other TLS slots can
|
||
sometimes cancel <em>all</em> destructors for the current thread, whether or not
|
||
the slots have already had their destructors run or not.</li>
|
||
</ol>
|
||
</div><h2 id='methods'>Methods</h2><h3 class='impl'><code>impl<T> <a class='struct' href='../../../bitflags/__core/thread/struct.LocalKey.html' title='bitflags::__core::thread::LocalKey'>LocalKey</a><T> <span class='where'>where T: 'static</span></code></h3><div class='impl-items'><h4 id='method.with' class='method'><code>fn <a href='#method.with' class='fnname'>with</a><F, R>(&'static self, f: F) -> R <span class='where'>where F: <a class='trait' href='../../../bitflags/__core/ops/trait.FnOnce.html' title='bitflags::__core::ops::FnOnce'>FnOnce</a>(&T) -> R</span></code></h4>
|
||
<div class='docblock'><p>Acquires a reference to the value in this TLS key.</p>
|
||
|
||
<p>This will lazily initialize the value if this thread has not referenced
|
||
this key yet.</p>
|
||
|
||
<h1 id='panics' class='section-header'><a href='#panics'>Panics</a></h1>
|
||
<p>This function will <code>panic!()</code> if the key currently has its
|
||
destructor running, and it <strong>may</strong> panic if the destructor has
|
||
previously been run for this thread.</p>
|
||
</div><h4 id='method.state' class='method'><code>fn <a href='#method.state' class='fnname'>state</a>(&'static self) -> <a class='enum' href='../../../bitflags/__core/thread/enum.LocalKeyState.html' title='bitflags::__core::thread::LocalKeyState'>LocalKeyState</a></code></h4>
|
||
<div class='stability'><em class='stab unstable'>Unstable (<code>thread_local_state</code>)<p>: state querying was recently added</p>
|
||
</em></div><div class='docblock'><p>Query the current state of this key.</p>
|
||
|
||
<p>A key is initially in the <code>Uninitialized</code> state whenever a thread
|
||
starts. It will remain in this state up until the first call to <code>with</code>
|
||
within a thread has run the initialization expression successfully.</p>
|
||
|
||
<p>Once the initialization expression succeeds, the key transitions to the
|
||
<code>Valid</code> state which will guarantee that future calls to <code>with</code> will
|
||
succeed within the thread.</p>
|
||
|
||
<p>When a thread exits, each key will be destroyed in turn, and as keys are
|
||
destroyed they will enter the <code>Destroyed</code> state just before the
|
||
destructor starts to run. Keys may remain in the <code>Destroyed</code> state after
|
||
destruction has completed. Keys without destructors (e.g. with types
|
||
that are <code>Copy</code>), may never enter the <code>Destroyed</code> state.</p>
|
||
|
||
<p>Keys in the <code>Uninitialized</code> state can be accessed so long as the
|
||
initialization does not panic. Keys in the <code>Valid</code> state are guaranteed
|
||
to be able to be accessed. Keys in the <code>Destroyed</code> state will panic on
|
||
any call to <code>with</code>.</p>
|
||
</div></div></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>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</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 = "bitflags";
|
||
window.playgroundUrl = "";
|
||
</script>
|
||
<script src="../../../jquery.js"></script>
|
||
<script src="../../../main.js"></script>
|
||
|
||
<script defer src="../../../search-index.js"></script>
|
||
</body>
|
||
</html> |