Crates.io | vslab |
lib.rs | vslab |
version | 2.0.0 |
created_at | 2025-09-22 07:31:05.135502+00 |
updated_at | 2025-09-22 08:29:47.555606+00 |
description | Provides a container with persistent unique keys to access stored values. |
homepage | |
repository | |
max_upload_size | |
id | 1849617 |
size | 43,143 |
Prodives a container with persistent unique keys to access stored values.
SlabId
traitstd
Add this to your Cargo.toml
:
[dependencies]
vslab = "*"
use vslab::Slab;
// Create a slab with u64 keys and String values
let mut slab = Slab::<u64, String>::default();
// Insert values and get unique keys
let apple_id = slab.insert("apple".to_string());
let banana_id = slab.insert("banana".to_string());
// Access values by key
assert_eq!(slab[apple_id], "apple");
assert_eq!(slab[banana_id], "banana");
// Remove a value
let removed = slab.remove(apple_id).unwrap();
assert_eq!(removed, "apple");
// The old key is now invalid (stale)
assert!(slab.get(apple_id).is_none());
// Insert a new value (reuses the old slot)
let orange_id = slab.insert("orange".to_string());
assert_eq!(slab[orange_id], "orange");
For better type safety, you can define custom key types:
use vslab::{Slab, new_type_id};
// Define a custom key type
new_type_id!(EntityId);
let mut entities = Slab::<EntityId, String>::default();
let player_id = entities.insert("Player".to_string());
let enemy_id = entities.insert("Enemy".to_string());
// Type safety: EntityId and u64 keys cannot be mixed
// let invalid: Slab<u64, String> = Slab::default();
// invalid.insert(player_id); // Compile error!
assert_eq!(entities[player_id], "Player");
assert_eq!(entities[enemy_id], "Enemy");
assert!(player_id.is_valid());
slotmap
and slab
cratesSee LICENSE file.