vslab 2.0.0

Provides a container with persistent unique keys to access stored values.
docs.rs failed to build vslab-2.0.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

🚀 Features

  • ⚡ O(1) Operations - Insert, remove, and access operations all run in constant time
  • 🔑 Versioned Keys - Keys include version information to prevent stale data access
  • 💾 Memory Efficient - Uses a free list to reuse memory of removed entries
  • 🔧 Custom Key Types - Support for user-defined key types via the SlabId trait
  • 📦 Zero Dependencies - Core functionality uses only std
  • 🌐 WASM Support - Full support for WebAssembly targets
  • 🛡️ Type Safe - Optional custom key types prevent mixing different key types

📦 Installation

Add this to your Cargo.toml:

[dependencies]
vslab = "*"

🎯 Basic Usage

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");

🔧 Custom Key Types

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());

🙏 Acknowledgments

  • Inspired by the slotmap and slab crates
  • Thanks to all contributors who have helped improve this crate

License

See LICENSE file.