3 releases (breaking)

✓ Uses Rust 2018 edition

0.4.0 Aug 25, 2019
0.3.0 Oct 28, 2018
0.2.1 Sep 16, 2018
0.2.0 Jul 17, 2018
0.1.1 Jul 10, 2018

#14 in Memory management

Download history 435/week @ 2019-07-16 509/week @ 2019-07-23 599/week @ 2019-07-30 621/week @ 2019-08-06 1084/week @ 2019-08-13 1379/week @ 2019-08-20 1231/week @ 2019-08-27 1163/week @ 2019-09-03 1268/week @ 2019-09-10 769/week @ 2019-09-17 1217/week @ 2019-09-24 2132/week @ 2019-10-01 1773/week @ 2019-10-08 2056/week @ 2019-10-15 1026/week @ 2019-10-22

2,469 downloads per month
Used in 31 crates (10 directly)

Zlib license

205KB
3.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 SparseSlotMap 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.

To start using slotmap add the following to your Cargo.toml:

[dependencies]
slotmap = "0.4"

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