4 releases (2 breaking)
0.2.0 | Jul 18, 2024 |
---|---|
0.1.1 | Dec 18, 2023 |
0.1.0 | Dec 11, 2023 |
0.0.0 | Nov 7, 2023 |
#16 in #slotmap
167 downloads per month
80KB
1.5K
SLoC
Riddance provides the Registry
container, which stores objects and issues unique IDs for
them, also known as a "slot map" or an "arena". Features include:
- New IDs can be "reserved" atomically, without locking the
Registry
. Seereserve_id
andreserve_ids
. - When the generation of a slot reaches its maximum, the slot is "retired" instead of allowing the generation to roll over to zero. This prevents logic errors from colliding IDs.
- The default [
Id
] type is 64 bits, but callers that need smallers IDs can useId32
, which has a configurable number of generation bits. - The
recycle_retired
method makes it possible to reuse previously retired slots, though it can introduce logic errors if you violate its contract. It's mainly intended for callers who useId32
. - By default ID types incorporate the
T
type parameter of theRegistry
that created them, to avoid confusing IDs from different registries.
Example
use riddance::{Id, Registry};
struct Person {
name: String,
friends: Vec<Id<Person>>,
}
let mut people = Registry::new();
let alice_id = people.insert(Person { name: "Alice".into(), friends: vec![] });
let bob_id = people.insert(Person { name: "Bob".into(), friends: vec![] });
people[alice_id].friends.push(bob_id);
people[bob_id].friends.push(alice_id);
people.remove(bob_id);
assert!(people.get(alice_id).is_some());
assert!(people.get(bob_id).is_none());
Dependencies
~2MB
~31K SLoC