crates

generational_table

check proofs license minimum Rust version

A fixed-capacity table that names each entry with a typed u64, so a name outlives nothing it named. Removing an entry raises its slot's generation, and every copy of the old name then resolves to None for good, whether or not the slot holds a new entry. The table is an array of N slots chosen at compile time: it is no_std, needs no allocator, has no dependency, and forbids unsafe code. Game engines call the same shape a slotmap.

Use it where you keep many short-lived records and hold references to them as plain words (in a packet, a register, an atomic, a file). It suits a program that needs a reference to a dead record to fail safely rather than dangle or reach somebody else's record. It is not for a collection that must grow past a capacity known up front; see the comparison below.

EXAMPLES

use generational_table::Table;

// Room for four sessions. `insert_with_name` hands the new entry its own name.
let mut sessions: Table<(u32, &str), 4> = Table::new();
let alice = sessions.insert_with_name(|name| (name.to_bits() as u32, "alice")).unwrap();
let bob = sessions.insert_with_name(|_| (0, "bob")).unwrap();
assert_eq!(sessions.get(alice).map(|s| s.1), Some("alice"));
assert_eq!(sessions.len(), 2);

// End Alice's session and let someone else take her slot.
assert!(sessions.remove(alice).is_some());
let carol = sessions.insert_with_name(|_| (0, "carol")).unwrap();

// Alice's old name does not reach Carol. It reaches nothing.
assert_ne!(alice, carol);
assert!(sessions.get(alice).is_none());
assert_eq!(sessions.get(carol).map(|s| s.1), Some("carol"));
assert_eq!(sessions.get(bob).map(|s| s.1), Some("bob"));

// A name crosses a boundary that carries a plain word, and comes back.
let word: u64 = carol.to_bits();
let again = generational_table::EntryName::<(u32, &str)>::from_bits(word);
assert_eq!(sessions.get(again).map(|s| s.1), Some("carol"));

// A word the table never minted resolves to nothing, whatever it holds.
assert!(sessions.get(generational_table::EntryName::NEVER_MINTED).is_none());

Table::<T, N>::new is a const fn, so a table can sit in a static. A name is an EntryName<T>, which packs the generation in the high 32 bits of a u64 and the slot in the low 32. It carries the entry type T as a tag only, so a name from a table of one entry type does not compile where a function wants another table's name. Two tables of the same entry type share a name type; give each its own entry type (a one-field wrapper is enough) to keep them apart.

Besides insert_with_name, get, get_mut and remove, the table has insert for a value that does not need its own name, contains_key, slot_index, len, is_empty and slot_count (N, for generic code). clear removes every entry and kills every name the table held. The walks are values, values_mut, live_slot_indices, iter and iter_mut (each name with its entry), and iter_from_slot_index, a sweep that resumes from a slot index after you give a lock back. The walks and clear cost what the table holds, not what it could hold.

Table<T, N> implements Clone when T: Clone, and Debug when T: Debug. A clone resolves exactly the names the original does. Debug prints the length, the slot count and the live entries only, each under its name, so the output does not grow with N. EntryName<T> implements Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash and Debug for every T. Names order by their packed value, generation first and then slot, so they key a BTreeMap and sort; the order says nothing about which entry came first.

What is proven

Ten Kani harnesses prove the safety claims. Kani is a model checker that proves properties for all inputs up to a stated size. The next section says how to run them.

Each harness has a falsification record at falsifications/verification.<harness>.patch. Applying it makes that harness fail at its own assertion, which shows the proof checks something. The proofs use tables of two or three slots and operation sequences of one or two steps. The host tests cover the rest: a name whose slot is past the table, a retired slot, a fully retired table, the hole below the walk bound, and an insert the table refuses without running its closure.

Building and proving

From the crate's directory, with a Rust toolchain from rustup:

cargo build
cargo test

cargo test runs the unit tests, the documentation tests (this README's example among them) and one test that must fail to compile.

The proofs need Kani, which rustup does not install. CI uses version 0.68.0, and another version may verify differently. The setup step downloads a model checker, so it needs network access.

cargo install --locked kani-verifier --version 0.68.0
cargo kani setup
cargo kani

A passing run ends with Complete - 10 successfully verified harnesses, 0 failures, 10 total. Above that line it also prints compiler warnings, a list of unsupported constructs and a few checks marked UNREACHABLE. None of those is a failure; each harness's own verdict is its VERIFICATION:- SUCCESSFUL line.

To watch a proof fail, apply its falsification patch, run that harness alone, then undo the patch:

git apply falsifications/verification.a_removed_name_never_resolves_again.patch
cargo kani --harness verification::a_removed_name_never_resolves_again --exact
git checkout -- src

The middle command ends with 0 successfully verified harnesses, 1 failures, 1 total., and the lines above it name the assertion that failed. CI replays every patch this way on each pull request.

The minimum Rust version is held by the weekly msrv job, which builds and tests at it; locally, rustup toolchain install 1.85 and then cargo +1.85 test do the same.

BUGS

How it differs from slab, slotmap, thunderdome, slots and coca

slab is where most programs meet this shape: a Vec of slots, each entry named by a plain usize with no generation. An index kept after its entry is removed reaches whatever the slot holds next, which is the mistake this crate's names refuse.

slotmap and thunderdome are the established, growable members of this family, and for most programs one of them is the right choice. They keep their entries in a Vec, so they need an allocator and grow without bound, and neither fixes its capacity at compile time. Their generations wrap, slotmap after 2^31 reuses of a slot and thunderdome after 2^32 - 1, so after that many reuses of one slot an ancient name can resolve to a new occupant. slotmap also lets a plain u64 convert into a key, and two such words can reach one entry. This crate trades growth for the opposite properties: a fixed array, no allocation, a const constructor, a generation that retires a slot rather than wrap, names that nobody can forge from a word, and machine-checked proofs of those claims. It costs you the capacity you choose up front and a slot now and then in a very long-lived table.

Two crates on crates.io also keep a fixed number of slots in an array, with no allocator. The notes below come from reading the published source of slots 0.4.0 and coca 0.3.0. Both versions date from March 2022.

slots is a fixed array of N slots with no generations. Its Slots type hands out a Key that nobody can copy or clone, and take consumes it. The compiler therefore rejects a second use of a removed key, which this crate checks only at run time. By default a key also records which Slots value made it and panics in another, where this crate lets a name from a second table of the same entry type resolve there. Its store and take run in constant time from a free list, where this crate's insert scans from its cursor. The price is that you cannot keep a key as a plain word. Key::index gives a usize, and try_read with that index reaches whatever the slot holds now, a later occupant included. Its UnrestrictedSlots type uses such indexes throughout. Its new is not const, it has no unsafe code of its own, and it depends on array-init. The published source has unit tests and no proofs or fuzzing.

coca is the closer match. Its DirectInlinePool and PackedInlinePool keep N entries inline, with no allocator unless you turn on its alloc feature. Its handles carry a generation, 32 bits in the default 64-bit handle, and as few as 16 in a 32-bit handle type from its handle_type! macro. That macro also gives each pool its own handle type. The generation wraps. Its documentation says so and shows a stale 16-bit handle reaching a new value after 32,768 reuses. Safe code cannot build its default handle from an integer, since the constructor is an unsafe function. This crate offers a safe from_bits instead and checks the generation on every lookup. coca inserts in constant time, its try_insert_with_handle matches insert_with_name, and its packed pool keeps values in one dense array. It also offers vectors, deques, maps and arenas. It uses unsafe code throughout its pools, and its changelog cites Miri, the Rust interpreter that detects undefined behavior. Its new is not const. The published source has tests and no proofs or fuzzing.

heapless 0.9.3 has no generational table. It has fixed-capacity vectors, maps and queues with const constructors, and its object pool hands out owning Object pointers rather than integer names. generational-arena 0.2.9 forbids unsafe code but keeps its entries in a Vec, and stable-vec 0.4.3 keeps a Vec with no generations.

Minimum Rust version

Rust 1.85.

License

Licensed under either of the MIT license (LICENSE-MIT) or the Apache License, Version 2.0 (LICENSE-APACHE), at your option.