Files
GopherGate/target/doc/slab/index.html
2026-02-26 12:00:21 -05:00

80 lines
9.9 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="Pre-allocated storage for a uniform data type."><title>slab - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="slab" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate slab</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../slab/index.html">slab</a><span class="version">0.4.12</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#performance-notes" title="Performance notes">Performance notes</a></li><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#capacity-and-reallocation" title="Capacity and reallocation">Capacity and reallocation</a></li><li><a href="#implementation" title="Implementation">Implementation</a></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>slab</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/slab/lib.rs.html#1-1653">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><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>
<h2 id="performance-notes"><a class="doc-anchor" href="#performance-notes">§</a>Performance notes</h2>
<p>Methods that remove values and return them, such as <a href="struct.Slab.html#method.remove" title="method slab::Slab::remove"><code>Slab::remove</code></a> and
<a href="struct.Slab.html#method.try_remove" title="method slab::Slab::try_remove"><code>Slab::try_remove</code></a>, might copy the removed values to the stack even if
their return values are unused. For types that dont have drop glue, the
compiler can usually elide these copies.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>Basic storing and retrieval.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>slab = Slab::new();
<span class="kw">let </span>hello = slab.insert(<span class="string">"hello"</span>);
<span class="kw">let </span>world = slab.insert(<span class="string">"world"</span>);
<span class="macro">assert_eq!</span>(slab[hello], <span class="string">"hello"</span>);
<span class="macro">assert_eq!</span>(slab[world], <span class="string">"world"</span>);
slab[world] = <span class="string">"earth"</span>;
<span class="macro">assert_eq!</span>(slab[world], <span class="string">"earth"</span>);</code></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"><code><span class="kw">let </span><span class="kw-2">mut </span>slab = Slab::new();
<span class="kw">let </span>hello = {
<span class="kw">let </span>entry = slab.vacant_entry();
<span class="kw">let </span>key = entry.key();
entry.insert((key, <span class="string">"hello"</span>));
key
};
<span class="macro">assert_eq!</span>(hello, slab[hello].<span class="number">0</span>);
<span class="macro">assert_eq!</span>(<span class="string">"hello"</span>, slab[hello].<span class="number">1</span>);</code></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"><code><span class="kw">let </span><span class="kw-2">mut </span>slab = Slab::with_capacity(<span class="number">1024</span>);
<span class="comment">// ... use the slab
</span><span class="kw">if </span>slab.len() == slab.capacity() {
<span class="macro">panic!</span>(<span class="string">"slab full"</span>);
}
slab.insert(<span class="string">"the slab is not at capacity yet"</span>);</code></pre></div><h2 id="capacity-and-reallocation"><a class="doc-anchor" href="#capacity-and-reallocation">§</a>Capacity and reallocation</h2>
<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 slabs 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>
<h2 id="implementation"><a class="doc-anchor" href="#implementation">§</a>Implementation</h2>
<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></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Drain.html" title="struct slab::Drain">Drain</a></dt><dd>A draining iterator for <code>Slab</code></dd><dt><a class="struct" href="struct.IntoIter.html" title="struct slab::IntoIter">Into<wbr>Iter</a></dt><dd>A consuming iterator over the values stored in a <code>Slab</code></dd><dt><a class="struct" href="struct.Iter.html" title="struct slab::Iter">Iter</a></dt><dd>An iterator over the values stored in the <code>Slab</code></dd><dt><a class="struct" href="struct.IterMut.html" title="struct slab::IterMut">IterMut</a></dt><dd>A mutable iterator over the values stored in the <code>Slab</code></dd><dt><a class="struct" href="struct.Slab.html" title="struct slab::Slab">Slab</a></dt><dd>Pre-allocated storage for a uniform data type</dd><dt><a class="struct" href="struct.VacantEntry.html" title="struct slab::VacantEntry">Vacant<wbr>Entry</a></dt><dd>A handle to a vacant entry in a <code>Slab</code>.</dd></dl><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.GetDisjointMutError.html" title="enum slab::GetDisjointMutError">GetDisjoint<wbr>MutError</a></dt><dd>The error type returned by <a href="struct.Slab.html#method.get_disjoint_mut" title="method slab::Slab::get_disjoint_mut"><code>Slab::get_disjoint_mut</code></a>.</dd></dl></section></div></main></body></html>