Expand description
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
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());
Modules
- error types
- additional ID types
- a secondary map from IDs to other values
- iterator types
Structs
- A container that issues IDs and maps them to stored values, also called a “slot map” or an “arena”.
Type Aliases
- The default 64-bit ID type.