mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-02-23 15:49:49 +01:00
89 lines
11 KiB
HTML
89 lines
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 `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="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings"></script><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../favicon.svg">
|
||
<link rel="alternate icon" type="image/png" href="../favicon-16x16.png">
|
||
<link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><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">☰</div><a href='../slab/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><p class="location">Crate slab</p><div class="block version"><p>Version 0.4.2</p></div><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><div id="sidebar-vars" data-name="slab" data-ty="mod" data-relpath="../"></div></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></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><button type="button" class="help-button">?</button>
|
||
<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="in-band">Crate <a class="mod" href="">slab</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 class="srclink" href="../src/slab/lib.rs.html#1-977" title="goto source code">[src]</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">"hello"</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">"world"</span>);
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">slab</span>[<span class="ident">hello</span>], <span class="string">"hello"</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">slab</span>[<span class="ident">world</span>], <span class="string">"world"</span>);
|
||
|
||
<span class="ident">slab</span>[<span class="ident">world</span>] <span class="op">=</span> <span class="string">"earth"</span>;
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">slab</span>[<span class="ident">world</span>], <span class="string">"earth"</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">"hello"</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">"hello"</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">"slab full"</span>);
|
||
}
|
||
|
||
<span class="ident">slab</span>.<span class="ident">insert</span>(<span class="string">"the slab is not at capacity yet"</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><div id="rustdoc-vars" data-root-path="../" data-current-crate="slab"></div>
|
||
<script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |