Skip to main content

Crate thunderation

Crate thunderation 

Source
Expand description

Thunderation is a generational arena forked from Thunderdome. It adds a few improvements like unique key type identifiers, improved methods, and the removal of all unsafe code.

It provides constant time insertion, lookup, and removal via small (8 byte) keys returned from Arena or via slot indices. Thunderation’s key type is still 8 bytes when put inside of an Option<Key> thanks to Rust’s NonZero* types. One limitation of this key type is that arenas can only hold up to u32::MAX elements.

This data structure is backed by a Vec and is implemented using zero unsafe code. No AI was used in any process of making this crate.

§Why use this over slotmap?

The main reason is when you need to get and set values by their slot index in addition to generational keys. If you do not need this, consider using slotmap instead.

§Basic Examples


// Define a unique key type identifier. This prevents keys from
// being used in Arena's they don't belong to.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct MyKey;

// Create an arena with the given key type and the storage type.
let mut arena = Arena::<MyKey, &'static str>::new();

let foo = arena.insert("Foo");
let bar = arena.insert("Bar");

assert_eq!(arena[foo], "Foo");
assert_eq!(arena[bar], "Bar");

// Methods to get/insert items by slot index are also available.
let (key, value) = arena.get_by_slot(foo.slot()).unwrap();
assert_eq!(key, foo);
assert_eq!(value, &"Foo");

arena[bar] = "Replaced";
assert_eq!(arena[bar], "Replaced");

let foo_value = arena.remove(foo);
assert_eq!(foo_value, Some("Foo"));

// The slot previously used by foo will be reused for baz
let baz = arena.insert("Baz");
assert_eq!(arena[baz], "Baz");

// foo is no longer a valid key
assert_eq!(arena.get(foo), None);

§Comparison With Similar Crates

FeatureThunderationThunderdomegenerational-arenatyped-generational-arenaslotmapslab
Generational¹YesYesYesYesYesNo
size_of::<Key>()8816varies88
size_of::<Option<Key>>()8824varies816
Max elements2³²2³²2⁶⁴2⁶⁴2³²2⁶⁴
Non-Copy valuesYesYesYesYesYesYes
no_std supportYesYesYesYesYesYes
Uniquely typed keysYesNoNoYesYesNo
Get/set values by slot indexYesYesNoNoNoYes
Zero unsafe usedYesNoYesYesNoNo
Serde supportNoNoYesYesYesNo
  1. Generational indices help solve the ABA Problem, which can cause dangling keys to mistakenly access newly-inserted data.

§Crate Features

  • std (default): Use the standard library. Disable to make this crate no-std compatible.

Modules§

iter
Contains all of the iterator types for Thunderation.

Structs§

Arena
Container that can have elements inserted into it and removed from it.
Key
A key type for an Arena that has a generation attached to it.