| Crates.io | keyed-lock |
| lib.rs | keyed-lock |
| version | 0.2.3 |
| created_at | 2025-07-19 18:49:02.411413+00 |
| updated_at | 2025-07-23 07:08:27.097967+00 |
| description | A keyed lock for synchronization. |
| homepage | |
| repository | https://github.com/ldm0/keyed-lock |
| max_upload_size | |
| id | 1760439 |
| size | 30,741 |
This crate provides a keyed lock, which allows you to lock a resource based on a key. This is useful when you have a collection of resources and you want to lock individual resources by their key.
This crate provides both synchronous and asynchronous implementations.
Add this to your Cargo.toml:
[dependencies]
keyed-lock = { version = "0.2", features = ["sync", "async"] }
The sync module provides a synchronous KeyedLock.
Locks with the same key will block each other, while locks with different keys will not.
use keyed_lock::sync::KeyedLock;
let lock = KeyedLock::new();
// Lock key "a", this will not block.
let _guard_a = lock.lock("a");
// Lock key "b", this will not block as it's a different key.
let _guard_b = lock.lock("b");
// Try to lock "a" again, this will block until `_guard_a` is dropped.
// let _guard_a2 = lock.lock("a");
The async module provides an asynchronous KeyedLock.
Locks with the same key will block each other, while locks with different keys will not.
use keyed_lock::r#async::KeyedLock;
let lock = KeyedLock::new();
// Lock key "a", this will not block.
let _guard_a = lock.lock("a").await;
// Lock key "b", this will not block as it's a different key.
let _guard_b = lock.lock("b").await;
// Try to lock "a" again, this will block until `_guard_a` is dropped.
// let _guard_a2 = lock.lock("a").await;
sync: Enables the synchronous KeyedLock.async: Enables the asynchronous KeyedLock.By default, both features are enabled.