4 releases (2 stable)

1.0.7 Nov 30, 2023
1.0.6 Aug 30, 2021
1.0.5 Jul 3, 2021
1.0.2 Jan 9, 2021
0.2.0 Jul 17, 2018

#13 in Data structures

Download history 179923/week @ 2024-08-14 189477/week @ 2024-08-21 205290/week @ 2024-08-28 216432/week @ 2024-09-04 196052/week @ 2024-09-11 199313/week @ 2024-09-18 206241/week @ 2024-09-25 215917/week @ 2024-10-02 199521/week @ 2024-10-09 230797/week @ 2024-10-16 221706/week @ 2024-10-23 233972/week @ 2024-10-30 352079/week @ 2024-11-06 377880/week @ 2024-11-13 352744/week @ 2024-11-20 208547/week @ 2024-11-27

1,339,521 downloads per month
Used in 3,107 crates (194 directly)

Zlib license

285KB
4.5K SLoC

slotmap

A Rust library providing three containers with persistent unique keys to access stored values, SlotMap, HopSlotMap and DenseSlotMap. Upon insertion a key is returned that can be used to later access or remove the values. Insertion, deletion and access all take O(1) time with low overhead. Great for storing collections of objects that need stable, safe references but have no clear ownership otherwise, such as game entities or graph nodes. Two secondary maps, SecondaryMap and SparseSecondaryMap are also provided that allow you to map further objects to the keys created by one of the slot maps. Please refer to the documentation for more information.

The minimum required stable Rust version for slotmap is 1.49. To start using slotmap add the following to your Cargo.toml:

[dependencies]
slotmap = "1.0"

Example

A short example:

use slotmap::{SlotMap, SecondaryMap};

let mut sm = SlotMap::new();
let foo = sm.insert("foo");  // Key generated on insert.
let bar = sm.insert("bar");
assert_eq!(sm[foo], "foo");
assert_eq!(sm[bar], "bar");

sm.remove(bar);
let reuse = sm.insert("reuse");  // Space from bar reused.
assert_eq!(sm.contains_key(bar), false);  // After deletion a key stays invalid.

let mut sec = SecondaryMap::new();
sec.insert(foo, "noun");  // We provide the key for secondary maps.
sec.insert(reuse, "verb");

for (key, val) in sm {
    println!("{} is a {}", val, sec[key]);
}

License

slotmap is released under the Zlib license, a permissive license. It is OSI and FSF approved and GPL compatible.

Dependencies