| Crates.io | itempool |
| lib.rs | itempool |
| version | 0.1.0 |
| created_at | 2025-11-18 12:09:48.283276+00 |
| updated_at | 2025-11-18 12:09:48.283276+00 |
| description | A simple trait for managing pools of reusable items with discard/recycle support. |
| homepage | |
| repository | https://github.com/caniko/itempool |
| max_upload_size | |
| id | 1938361 |
| size | 45,580 |
A lightweight Rust library for managing pools of reusable items with support for random selection, unique set retrieval, and item recycling.
remove_one()remove_set()Hash + Eq + Syncwasm32-unknown-unknown targetAdd itempool to your Cargo.toml:
[dependencies]
itempool = "0.1"
PoolItem TraitItems in the pool must implement PoolItem, which requires:
Hash: For use in hash sets and uniqueness checksEq: For equality comparisonsSync: For safe concurrent access across threadsItemPool<T> TraitThe main trait for managing a pool of items. Provides:
pool(&mut self) -> &mut Vec<T>: Access to the main item storageput(&mut self, item: T): Add an item to the poolremove_one(&mut self) -> Option<T>: Remove and return one random itemremove_many(&mut self, size: usize) -> Vec<T>: Remove multiple items (duplicates possible)remove_set(&mut self, size: usize) -> HashSet<T>: Remove a set of unique itemsRecyclingItemPool<T> TraitExtends ItemPool with discard/recycle functionality:
get_discard_pool(&mut self) -> &mut Vec<T>: Access to the discard storagediscard_one(&mut self, item: T): Move an item to the discard poolrecycle_discarded(&mut self): Move all discarded items back to the main poolget_set(&mut self, size: usize) -> HashSet<T>: Get unique items, auto-recycling firstuse itempool::{ItemPool, RecyclingItemPool};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct EntityId(u32);
struct EnemyPool {
available: Vec<EntityId>,
discarded: Vec<EntityId>,
}
impl ItemPool<EntityId> for EnemyPool {
fn pool(&mut self) -> &mut Vec<EntityId> {
&mut self.available
}
}
impl RecyclingItemPool<EntityId> for EnemyPool {
fn get_discard_pool(&mut self) -> &mut Vec<EntityId> {
&mut self.discarded
}
}
fn main() {
let mut pool = EnemyPool {
available: (0..10).map(EntityId).collect(),
discarded: vec![],
};
// Get a single random enemy
if let Some(enemy) = pool.remove_one() {
println!("Spawned enemy: {:?}", enemy);
}
// Get a squad of 3 unique enemies
let squad = pool.remove_set(3);
println!("Enemy squad: {:?}", squad);
// Discard an enemy temporarily
pool.discard_one(EntityId(5));
// Get more enemies (automatically recycles discarded ones)
let reinforcements = pool.get_set(2);
println!("Reinforcements: {:?}", reinforcements);
}
See the examples directory for more detailed usage.
Contributions are welcome! Please feel free to submit issues or pull requests on GitHub.
Dual-licensed under MIT or Apache License 2.0.