key_mutex/lib.rs
1//! A concurrent, lock-free version of `HashMap<K, Mutex<V>>` and `HashMap<K, RwLock<V>>`.
2//!
3//! It automatically allocates mutexes and rwlocks for you, and it automatically deallocates
4//! them when they are no longer in use. The "no longer in use" condition is when the last
5//! guard is dropped AND [`Empty::is_empty`] returns `true`.
6//!
7//! It's a bit like [Web Locks API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API)
8//! when `V` is `()`.
9//!
10//! Tokio version is available behind `tokio` feature.
11//!
12//! # Example
13//!
14//! ```
15//! use std::sync::Arc;
16//! use key_mutex::KeyMutex;
17//!
18//! fn main() {
19//! let locks = KeyMutex::<u32, BTreeSet<String>>::new();
20//! let mut lock = locks.lock(1).unwrap();
21//! lock.insert("Hello".to_owned());
22//! lock.insert("World".to_owned());
23//! drop(lock);
24//!
25//! // Value is not empty and thus is not dropped
26//! assert_eq!(locks.len(), 1);
27//!
28//! let mut lock = locks.lock(1).unwrap();
29//! assert_eq!(lock.len(), 2);
30//! lock.clear();
31//! drop(lock);
32//!
33//! // Should be dropped now
34//! assert_eq!(locks.len(), 0);
35//! }
36
37mod empty;
38#[macro_use]
39mod inner;
40
41#[cfg(feature = "std")]
42pub mod std;
43#[cfg(feature = "tokio")]
44pub mod tokio;
45
46pub use empty::Empty;
47#[cfg(feature = "std")]
48pub use std::*;