You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/doc/slab/index.html

85 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!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 `slab` crate."><meta name="keywords" content="rust, rustlang, rust-lang, slab"><title>slab - 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='../slab/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Crate slab</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all slab's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'slab', 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"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" 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 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='../src/slab/lib.rs.html#1-977' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>slab</a></span></h1><div class='docblock'><p>Pre-allocated storage for a uniform data type.</p>
<p><code>Slab</code> provides pre-allocated storage for a single data type. If many values
of a single type are being allocated, it can be more efficient to
pre-allocate the necessary storage. Since the size of the type is uniform,
memory fragmentation can be avoided. Storing, clearing, and lookup
operations become very cheap.</p>
<p>While <code>Slab</code> may look like other Rust collections, it is not intended to be
used as a general purpose collection. The primary difference between <code>Slab</code>
and <code>Vec</code> is that <code>Slab</code> returns the key when storing the value.</p>
<p>It is important to note that keys may be reused. In other words, once a
value associated with a given key is removed from a slab, that key may be
returned from future calls to <code>insert</code>.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Basic storing and retrieval.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">slab</span> <span class="op">=</span> <span class="ident">Slab</span>::<span class="ident">new</span>();
<span class="kw">let</span> <span class="ident">hello</span> <span class="op">=</span> <span class="ident">slab</span>.<span class="ident">insert</span>(<span class="string">&quot;hello&quot;</span>);
<span class="kw">let</span> <span class="ident">world</span> <span class="op">=</span> <span class="ident">slab</span>.<span class="ident">insert</span>(<span class="string">&quot;world&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">slab</span>[<span class="ident">hello</span>], <span class="string">&quot;hello&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">slab</span>[<span class="ident">world</span>], <span class="string">&quot;world&quot;</span>);
<span class="ident">slab</span>[<span class="ident">world</span>] <span class="op">=</span> <span class="string">&quot;earth&quot;</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">slab</span>[<span class="ident">world</span>], <span class="string">&quot;earth&quot;</span>);</pre></div>
<p>Sometimes it is useful to be able to associate the key with the value being
inserted in the slab. This can be done with the <code>vacant_entry</code> API as such:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">slab</span> <span class="op">=</span> <span class="ident">Slab</span>::<span class="ident">new</span>();
<span class="kw">let</span> <span class="ident">hello</span> <span class="op">=</span> {
<span class="kw">let</span> <span class="ident">entry</span> <span class="op">=</span> <span class="ident">slab</span>.<span class="ident">vacant_entry</span>();
<span class="kw">let</span> <span class="ident">key</span> <span class="op">=</span> <span class="ident">entry</span>.<span class="ident">key</span>();
<span class="ident">entry</span>.<span class="ident">insert</span>((<span class="ident">key</span>, <span class="string">&quot;hello&quot;</span>));
<span class="ident">key</span>
};
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">hello</span>, <span class="ident">slab</span>[<span class="ident">hello</span>].<span class="number">0</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&quot;hello&quot;</span>, <span class="ident">slab</span>[<span class="ident">hello</span>].<span class="number">1</span>);</pre></div>
<p>It is generally a good idea to specify the desired capacity of a slab at
creation time. Note that <code>Slab</code> will grow the internal capacity when
attempting to insert a new value once the existing capacity has been reached.
To avoid this, add a check.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">slab</span> <span class="op">=</span> <span class="ident">Slab</span>::<span class="ident">with_capacity</span>(<span class="number">1024</span>);
<span class="comment">// ... use the slab</span>
<span class="kw">if</span> <span class="ident">slab</span>.<span class="ident">len</span>() <span class="op">=</span><span class="op">=</span> <span class="ident">slab</span>.<span class="ident">capacity</span>() {
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">&quot;slab full&quot;</span>);
}
<span class="ident">slab</span>.<span class="ident">insert</span>(<span class="string">&quot;the slab is not at capacity yet&quot;</span>);</pre></div>
<h1 id="capacity-and-reallocation" class="section-header"><a href="#capacity-and-reallocation">Capacity and reallocation</a></h1>
<p>The capacity of a slab is the amount of space allocated for any future
values that will be inserted in the slab. This is not to be confused with
the <em>length</em> of the slab, which specifies the number of actual values
currently being inserted. If a slab's length is equal to its capacity, the
next value inserted into the slab will require growing the slab by
reallocating.</p>
<p>For example, a slab with capacity 10 and length 0 would be an empty slab
with space for 10 more stored values. Storing 10 or fewer elements into the
slab will not change its capacity or cause reallocation to occur. However,
if the slab length is increased to 11 (due to another <code>insert</code>), it will
have to reallocate, which can be slow. For this reason, it is recommended to
use <a href="struct.Slab.html#with_capacity"><code>Slab::with_capacity</code></a> whenever possible to specify how many values the
slab is expected to store.</p>
<h1 id="implementation" class="section-header"><a href="#implementation">Implementation</a></h1>
<p><code>Slab</code> is backed by a <code>Vec</code> of slots. Each slot is either occupied or
vacant. <code>Slab</code> maintains a stack of vacant slots using a linked list. To
find a vacant slot, the stack is popped. When a slot is released, it is
pushed onto the stack.</p>
<p>If there are no more available slots in the stack, then <code>Vec::reserve(1)</code> is
called and a new slot is created.</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.Drain.html" title='slab::Drain struct'>Drain</a></td><td class='docblock-short'><p>A draining iterator for <code>Slab</code></p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Iter.html" title='slab::Iter struct'>Iter</a></td><td class='docblock-short'><p>An iterator over the values stored in the <code>Slab</code></p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.IterMut.html" title='slab::IterMut struct'>IterMut</a></td><td class='docblock-short'><p>A mutable iterator over the values stored in the <code>Slab</code></p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Slab.html" title='slab::Slab struct'>Slab</a></td><td class='docblock-short'><p>Pre-allocated storage for a uniform data type</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.VacantEntry.html" title='slab::VacantEntry struct'>VacantEntry</a></td><td class='docblock-short'><p>A handle to a vacant entry in a <code>Slab</code>.</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>&#9166;</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><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "slab";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>