Purse: Bag data structure implementation in Rust with lock-free, atomic features
Overview
Purse implements a bag in Rust, also known as a multi set, but that's a much more boring name. Bags are quite versatile structures, allowing storage of heterogeneous and non-unique collections of items, supporting different types and allowing duplicates.
The Purse crate provides an atomic, lock-free version of a bag via the atomic feature, which is thread-safe and backed by the popular dashmap.
Key Features
- Versatility: Can contain any item, with duplicates and different types.
- Mixed Collections: Ideal for applications requiring a mix of different types.
- Duplicate Handling: Allows multiple instances of the same item.
Crate Variants
Purse comes in two flavors:
- Standard Implementation: Utilizes HashMap from Rust's standard library.
- Atomic Variant: Enabled by the
atomicfeature flag, uses DashMap for thread-safe operations without locks.
Requirements
Types stored must implement the Any trait. For the atomic variant, types also need Send and Sync.
Usage
use TypeId;
use Purse;
let mut purse = new;
purse.insert;
purse.insert;
assert!;
assert!;
assert_eq!;
// Get all of type.
let strings: = purse.get_all_of_type;
assert_eq!;
purse.insert;
purse.insert;
purse.insert;
assert!;
assert!;
assert!;
// Check the most common type.
assert_eq!;
// Clearing the bag.
purse.clear;
assert!;
// Add a few items again.
purse.insert;
purse.insert;
// Iteration over all elements in the bag
let mut nums: = vec!;
let mut strs: = vec!;
purse.iter.for_each;
assert_eq!;
assert_eq!;
Add purse to your project
cargo add purse
or enable the atomic feature for thread safety:
cargo add purse --features=atomic
Contributing
Contributions are welcome! Please follow the standard Rust conventions for pull requests.
License
This project is licensed under either of
- Apache License, Version 2.0 (licenses/Apache-2.0)
- MIT license (licenses/MIT)
at your option.
The SPDX license identifier for this project is MIT OR Apache-2.0.