Files
mercator_service/nom/lib/std/boxed/index.html

95 lines
12 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 `boxed` mod in crate `nom`."><meta name="keywords" content="rust, rustlang, rust-lang, boxed"><title>nom::lib::std::boxed - 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="../../../../light.css" id="themeStyle"><script src="../../../../storage.js"></script><noscript><link rel="stylesheet" href="../../../../noscript.css"></noscript><link rel="shortcut icon" href="../../../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../../../down-arrow.svg");}</style></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><a href='../../../../nom/index.html'><div class='logo-container'><img src='../../../../rust-logo.png' alt='logo'></div></a><p class='location'>Module boxed</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'>nom</a>::<wbr><a href='../../index.html'>lib</a>::<wbr><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'boxed', 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"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><a id="settings-menu" href="../../../../settings.html"><img src="../../../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><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='https://doc.rust-lang.org/nightly/src/alloc/lib.rs.html#147' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../../../index.html'>nom</a>::<wbr><a href='../../index.html'>lib</a>::<wbr><a href='../index.html'>std</a>::<wbr><a class="mod" href=''>boxed</a></span></h1><div class='docblock'><p>A pointer type for heap allocation.</p>
<p><a href="struct.Box.html"><code>Box&lt;T&gt;</code></a>, casually referred to as a 'box', provides the simplest form of
heap allocation in Rust. Boxes provide ownership for this allocation, and
drop their contents when they go out of scope. Boxes also ensure that they
never allocate more than <code>isize::MAX</code> bytes.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Move a value from the stack to the heap by creating a <a href="struct.Box.html"><code>Box</code></a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">val</span>: <span class="ident">u8</span> <span class="op">=</span> <span class="number">5</span>;
<span class="kw">let</span> <span class="ident">boxed</span>: <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">val</span>);</pre></div>
<p>Move a value from a <a href="struct.Box.html"><code>Box</code></a> back to the stack by <a href="../../std/ops/trait.Deref.html">dereferencing</a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">boxed</span>: <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Box</span>::<span class="ident">new</span>(<span class="number">5</span>);
<span class="kw">let</span> <span class="ident">val</span>: <span class="ident">u8</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">boxed</span>;</pre></div>
<p>Creating a recursive data structure:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>)]</span>
<span class="kw">enum</span> <span class="ident">List</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">Cons</span>(<span class="ident">T</span>, <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">List</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span><span class="op">&gt;</span>),
<span class="ident">Nil</span>,
}
<span class="kw">let</span> <span class="ident">list</span>: <span class="ident">List</span><span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">List</span>::<span class="ident">Cons</span>(<span class="number">1</span>, <span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">List</span>::<span class="ident">Cons</span>(<span class="number">2</span>, <span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">List</span>::<span class="ident">Nil</span>))));
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">list</span>);</pre></div>
<p>This will print <code>Cons(1, Cons(2, Nil))</code>.</p>
<p>Recursive structures must be boxed, because if the definition of <code>Cons</code>
looked like this:</p>
<div class='information'><div class='tooltip compile_fail'><span class='tooltiptext'>This example deliberately fails to compile</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail">
<span class="ident">Cons</span>(<span class="ident">T</span>, <span class="ident">List</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>),</pre></div>
<p>It wouldn't work. This is because the size of a <code>List</code> depends on how many
elements are in the list, and so we don't know how much memory to allocate
for a <code>Cons</code>. By introducing a <a href="struct.Box.html"><code>Box&lt;T&gt;</code></a>, which has a defined size, we know how
big <code>Cons</code> needs to be.</p>
<h1 id="memory-layout" class="section-header"><a href="#memory-layout">Memory layout</a></h1>
<p>For non-zero-sized values, a <a href="struct.Box.html"><code>Box</code></a> will use the <a href="../alloc/struct.Global.html"><code>Global</code></a> allocator for
its allocation. It is valid to convert both ways between a <a href="struct.Box.html"><code>Box</code></a> and a
raw pointer allocated with the <a href="../alloc/struct.Global.html"><code>Global</code></a> allocator, given that the
<a href="../alloc/struct.Layout.html"><code>Layout</code></a> used with the allocator is correct for the type. More precisely,
a <code>value: *mut T</code> that has been allocated with the <a href="../alloc/struct.Global.html"><code>Global</code></a> allocator
with <code>Layout::for_value(&amp;*value)</code> may be converted into a box using
<a href="struct.Box.html#method.from_raw"><code>Box::&lt;T&gt;::from_raw(value)</code></a>. Conversely, the memory backing a <code>value: *mut T</code> obtained from <a href="struct.Box.html#method.into_raw"><code>Box::&lt;T&gt;::into_raw</code></a> may be deallocated using the
<a href="../alloc/struct.Global.html"><code>Global</code></a> allocator with <a href="../alloc/struct.Layout.html#method.for_value"><code>Layout::for_value(&amp;*value)</code></a>.</p>
<p>So long as <code>T: Sized</code>, a <code>Box&lt;T&gt;</code> is guaranteed to be represented
as a single pointer and is also ABI-compatible with C pointers
(i.e. the C type <code>T*</code>). This means that if you have extern &quot;C&quot;
Rust functions that will be called from C, you can define those
Rust functions using <code>Box&lt;T&gt;</code> types, and use <code>T*</code> as corresponding
type on the C side. As an example, consider this C header which
declares functions that create and destroy some kind of <code>Foo</code>
value:</p>
<pre><code class="language-c">/* C header */
/* Returns ownership to the caller */
struct Foo* foo_new(void);
/* Takes ownership from the caller; no-op when invoked with NULL */
void foo_delete(struct Foo*);
</code></pre>
<p>These two functions might be implemented in Rust as follows. Here, the
<code>struct Foo*</code> type from C is translated to <code>Box&lt;Foo&gt;</code>, which captures
the ownership constraints. Note also that the nullable argument to
<code>foo_delete</code> is represented in Rust as <code>Option&lt;Box&lt;Foo&gt;&gt;</code>, since <code>Box&lt;Foo&gt;</code>
cannot be null.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="attribute">#[<span class="ident">repr</span>(<span class="ident">C</span>)]</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Foo</span>;
<span class="attribute">#[<span class="ident">no_mangle</span>]</span>
<span class="kw">pub</span> <span class="kw">extern</span> <span class="string">&quot;C&quot;</span> <span class="kw">fn</span> <span class="ident">foo_new</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">Foo</span><span class="op">&gt;</span> {
<span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">Foo</span>)
}
<span class="attribute">#[<span class="ident">no_mangle</span>]</span>
<span class="kw">pub</span> <span class="kw">extern</span> <span class="string">&quot;C&quot;</span> <span class="kw">fn</span> <span class="ident">foo_delete</span>(<span class="kw">_</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Box</span><span class="op">&lt;</span><span class="ident">Foo</span><span class="op">&gt;</span><span class="op">&gt;</span>) {}</pre></div>
<p>Even though <code>Box&lt;T&gt;</code> has the same representation and C ABI as a C pointer,
this does not mean that you can convert an arbitrary <code>T*</code> into a <code>Box&lt;T&gt;</code>
and expect things to work. <code>Box&lt;T&gt;</code> values will always be fully aligned,
non-null pointers. Moreover, the destructor for <code>Box&lt;T&gt;</code> will attempt to
free the value with the global allocator. In general, the best practice
is to only use <code>Box&lt;T&gt;</code> for pointers that originated from the global
allocator.</p>
<p><strong>Important.</strong> At least at present, you should avoid using
<code>Box&lt;T&gt;</code> types for functions that are defined in C but invoked
from Rust. In those cases, you should directly mirror the C types
as closely as possible. Using types like <code>Box&lt;T&gt;</code> where the C
definition is just using <code>T*</code> can lead to undefined behavior, as
described in <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/198">rust-lang/unsafe-code-guidelines#198</a>.</p>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.Box.html" title='nom::lib::std::boxed::Box struct'>Box</a></td><td class='docblock-short'><p>A pointer type for heap allocation.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../../../";window.currentCrate = "nom";</script><script src="../../../../aliases.js"></script><script src="../../../../main.js"></script><script defer src="../../../../search-index.js"></script></body></html>