11 releases

0.3.3 Jun 14, 2024
0.3.2 Jun 2, 2024
0.2.0 May 28, 2024
0.1.8 Mar 15, 2024
0.1.0 Sep 8, 2023

#344 in Data structures

Download history 19/week @ 2024-03-02 326/week @ 2024-03-09 72/week @ 2024-03-16 8/week @ 2024-03-23 24/week @ 2024-03-30 3/week @ 2024-04-06 5/week @ 2024-04-13 1/week @ 2024-05-18 164/week @ 2024-05-25 514/week @ 2024-06-01 134/week @ 2024-06-08 52/week @ 2024-06-15

864 downloads per month
Used in pi_world

MIT/Apache

30KB
440 lines

Thread-safe slotmap

slotmap

A Rust library providing three containers with persistent unique keys to access stored values. 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. Key maps, KeyMap are also provided that allow you to map further objects to the keys created by one of the slot maps. Please refer to the the documentation for more information.

[dependencies]
pi_slot = "0.1"

Example

A short example:

use slotmap::SlotMap;

let 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 sec = KeyMap::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

~385KB