oxipng/doc/bitflags/__core/mem/fn.uninitialized.html
2016-05-04 10:41:29 -04:00

187 lines
No EOL
9.4 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 `uninitialized` fn in crate `bitflags`.">
<meta name="keywords" content="rust, rustlang, rust-lang, uninitialized">
<title>bitflags::__core::mem::uninitialized - 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'>mem</a></p><script>window.sidebarCurrent = {name: 'uninitialized', ty: 'fn', 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 fn">
<h1 class='fqn'><span class='in-band'>Function <a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a href='index.html'>mem</a>::<wbr><a class='fn' href=''>uninitialized</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 id='src-5609' class='srclink' href='https://doc.rust-lang.org/nightly/core/mem/fn.uninitialized.html?gotosrc=5609' title='goto source code'>[src]</a></span></h1>
<pre class='rust fn'>pub unsafe fn uninitialized&lt;T&gt;() -&gt; T</pre><span class="since">1.0.0</span><div class='docblock'><p>Bypasses Rust&#39;s normal memory-initialization checks by pretending to
produce a value of type T, while doing nothing at all.</p>
<p><strong>This is incredibly dangerous, and should not be done lightly. Deeply
consider initializing your memory with a default value instead.</strong></p>
<p>This is useful for FFI functions and initializing arrays sometimes,
but should generally be avoided.</p>
<h1 id='undefined-behavior' class='section-header'><a href='#undefined-behavior'>Undefined Behavior</a></h1>
<p>It is Undefined Behavior to read uninitialized memory. Even just an
uninitialized boolean. For instance, if you branch on the value of such
a boolean your program may take one, both, or neither of the branches.</p>
<p>Note that this often also includes <em>writing</em> to the uninitialized value.
Rust believes the value is initialized, and will therefore try to Drop
the uninitialized value and its fields if you try to overwrite the memory
in a normal manner. The only way to safely initialize an arbitrary
uninitialized value is with one of the <code>ptr</code> functions: <code>write</code>, <code>copy</code>, or
<code>copy_nonoverlapping</code>. This isn&#39;t necessary if <code>T</code> is a primitive
or otherwise only contains types that don&#39;t implement Drop.</p>
<p>If this value <em>does</em> need some kind of Drop, it must be initialized before
it goes out of scope (and therefore would be dropped). Note that this
includes a <code>panic</code> occurring and unwinding the stack suddenly.</p>
<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
<p>Here&#39;s how to safely initialize an array of <code>Vec</code>s.</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>mem</span>;
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>ptr</span>;
<span class='comment'>// Only declare the array. This safely leaves it</span>
<span class='comment'>// uninitialized in a way that Rust will track for us.</span>
<span class='comment'>// However we can&#39;t initialize it element-by-element</span>
<span class='comment'>// safely, and we can&#39;t use the `[value; 1000]`</span>
<span class='comment'>// constructor because it only works with `Copy` data.</span>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>data</span>: [<span class='ident'>Vec</span><span class='op'>&lt;</span><span class='ident'>u32</span><span class='op'>&gt;</span>; <span class='number'>1000</span>];
<span class='kw'>unsafe</span> {
<span class='comment'>// So we need to do this to initialize it.</span>
<span class='ident'>data</span> <span class='op'>=</span> <span class='ident'>mem</span>::<span class='ident'>uninitialized</span>();
<span class='comment'>// DANGER ZONE: if anything panics or otherwise</span>
<span class='comment'>// incorrectly reads the array here, we will have</span>
<span class='comment'>// Undefined Behavior.</span>
<span class='comment'>// It&#39;s ok to mutably iterate the data, since this</span>
<span class='comment'>// doesn&#39;t involve reading it at all.</span>
<span class='comment'>// (ptr and len are statically known for arrays)</span>
<span class='kw'>for</span> <span class='ident'>elem</span> <span class='kw'>in</span> <span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>data</span>[..] {
<span class='comment'>// *elem = Vec::new() would try to drop the</span>
<span class='comment'>// uninitialized memory at `elem` -- bad!</span>
<span class='comment'>//</span>
<span class='comment'>// Vec::new doesn&#39;t allocate or do really</span>
<span class='comment'>// anything. It&#39;s only safe to call here</span>
<span class='comment'>// because we know it won&#39;t panic.</span>
<span class='ident'>ptr</span>::<span class='ident'>write</span>(<span class='ident'>elem</span>, <span class='ident'>Vec</span>::<span class='ident'>new</span>());
}
<span class='comment'>// SAFE ZONE: everything is initialized.</span>
}
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{:?}&quot;</span>, <span class='kw-2'>&amp;</span><span class='ident'>data</span>[<span class='number'>0</span>]);</pre>
<p>This example emphasizes exactly how delicate and dangerous doing this is.
Note that the <code>vec!</code> macro <em>does</em> let you initialize every element with a
value that is only <code>Clone</code>, so the following is semantically equivalent and
vastly less dangerous, as long as you can live with an extra heap
allocation:</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>let</span> <span class='ident'>data</span>: <span class='ident'>Vec</span><span class='op'>&lt;</span><span class='ident'>Vec</span><span class='op'>&lt;</span><span class='ident'>u32</span><span class='op'>&gt;&gt;</span> <span class='op'>=</span> <span class='macro'>vec</span><span class='macro'>!</span>[<span class='ident'>Vec</span>::<span class='ident'>new</span>(); <span class='number'>1000</span>];
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{:?}&quot;</span>, <span class='kw-2'>&amp;</span><span class='ident'>data</span>[<span class='number'>0</span>]);</pre>
</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>&larrb;</dt>
<dd>Move up in search results</dd>
<dt>&rarrb;</dt>
<dd>Move down in search results</dd>
<dt>&#9166;</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>