Keyed Lock
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.
Usage
Add this to your Cargo.toml:
[]
= { = "0.2", = ["sync", "async"] }
Synchronous
The sync module provides a synchronous KeyedLock.
Locks with the same key will block each other, while locks with different keys will not.
use KeyedLock;
let lock = new;
// Lock key "a", this will not block.
let _guard_a = lock.lock;
// Lock key "b", this will not block as it's a different key.
let _guard_b = lock.lock;
// Try to lock "a" again, this will block until `_guard_a` is dropped.
// let _guard_a2 = lock.lock("a");
Asynchronous
The async module provides an asynchronous KeyedLock.
Locks with the same key will block each other, while locks with different keys will not.
use r#KeyedLock;
let lock = new;
// Lock key "a", this will not block.
let _guard_a = lock.lock.await;
// Lock key "b", this will not block as it's a different key.
let _guard_b = lock.lock.await;
// Try to lock "a" again, this will block until `_guard_a` is dropped.
// let _guard_a2 = lock.lock("a").await;
Features
sync: Enables the synchronousKeyedLock.async: Enables the asynchronousKeyedLock.
By default, both features are enabled.