https://github.com/flixcoder/key-lock
Library for mutual exclusion by keys.
https://github.com/flixcoder/key-lock
locks rust
Last synced: 6 months ago
JSON representation
Library for mutual exclusion by keys.
- Host: GitHub
- URL: https://github.com/flixcoder/key-lock
- Owner: FlixCoder
- License: mit
- Created: 2022-12-22T10:05:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-06T14:44:38.000Z (12 months ago)
- Last Synced: 2025-06-23T01:03:55.115Z (9 months ago)
- Topics: locks, rust
- Language: Rust
- Homepage: https://crates.io/crates/key-lock
- Size: 22.5 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# KeyLock
[](https://crates.io/crates/key-lock)
[](https://docs.rs/key-lock/)

Simple library for mutual exclusion based on keys. Lock and wait for execution by key.
## Usage
Import the project using:
```bash
cargo add key-lock
```
## Example
```rust
use key_lock::KeyLock;
#[tokio::main]
async fn main() {
// Initialize new lock.
let lock = KeyLock::new();
// Lock A, continues immediately.
let _a = lock.lock("a").await;
// Lock B, continues immediately.
let _b = lock.lock("b").await;
// Try to lock A, but it is already locked. Normal locking would block here.
assert!(lock.try_lock("a").await.is_err());
}
```
## Minimum supported Rust version
Currently, I am always using the latest stable Rust version and do not put in effort to keep the MSRV. Please open an issue in case you need a different policy, I might consider changing the policy.
## License
Licensed under the MIT license. All contributors agree to license under this license.