itempool
A lightweight Rust library for managing pools of reusable items with support for random selection, unique set retrieval, and item recycling.
Features
- Random Item Selection: Efficiently retrieve random items from a pool with
remove_one() - Unique Sets: Get sets of unique items without duplicates using
remove_set() - Item Recycling: Temporarily discard items and recycle them back into the pool
- Type Safety: Uses trait bounds ensuring items are
Hash + Eq + Sync - Zero-cost Abstractions: Trait-based design with minimal overhead
- WebAssembly Support: Works with
wasm32-unknown-unknowntarget
Installation
Add itempool to your Cargo.toml:
[]
= "0.1"
Core Concepts
PoolItem Trait
Items 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 threads
ItemPool<T> Trait
The 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 items
RecyclingItemPool<T> Trait
Extends 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 first
Example
use ;
;
See the examples directory for more detailed usage.
Use Cases
- Game Development: Managing enemy spawns, item drops, card decks
- Resource Pooling: Reusable connection pools, object pools
- Random Selection: Lottery systems, random sampling without replacement
- State Management: Temporarily unavailable resources that can be restored
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests on GitHub.
License
Dual-licensed under MIT or Apache License 2.0.