| Crates.io | key-mutex |
| lib.rs | key-mutex |
| version | 0.1.3 |
| created_at | 2024-03-18 06:26:47.976005+00 |
| updated_at | 2024-04-06 02:53:06.745712+00 |
| description | Access mutexes by key |
| homepage | |
| repository | https://github.com/Mivik/key-mutex |
| max_upload_size | |
| id | 1177220 |
| size | 16,522 |
KeyMutex<K, V> is basically a lock-free HashMap<K, Mutex<V>> (or RwLock) with a few extra features.
Imagine you have a bunch of users identified by their IDs, and you want to have a lock for each user. That's when a KeyMutex<K, ()> comes in handy. The difference from DashMap<K, Mutex<()>> is that KeyMutex will automatically release the entry when the last mutex guard is dropped.
Things might get trickier when you want to, say, maintain a user's active sessions. You need to have a KeyMutex<K, Vec<Session>>, and you certainly don't want the entry be dropped unless the sessions are empty. Don't worry, KeyMutex handles this for you. The entry will be dropped only when Empty::is_empty returns true, which is implemented for all collections in the standard library and also, of course, for KeyMutex.