key-lock

Crates.iokey-lock
lib.rskey-lock
version0.1.0
sourcesrc
created_at2022-12-22 10:21:37.518393
updated_at2022-12-22 10:21:37.518393
descriptionLibrary for mutual exclusion by keys.
homepagehttps://github.com/FlixCoder/key-lock
repositoryhttps://github.com/FlixCoder/key-lock
max_upload_size
id743807
size12,036
Flix (FlixCoder)

documentation

https://docs.rs/key-lock

README

KeyLock

crates.io page docs.rs page license: MIT

Simple library for mutual exclusion based on keys. Lock and wait for execution by key.

Usage

Import the project using:

cargo add key-lock

Example

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());
}

Lints

This projects uses a bunch of clippy lints for higher code quality and style.

Install cargo-lints using cargo install --git https://github.com/FlixCoder/cargo-lints. The lints are defined in lints.toml and can be checked by running cargo lints clippy --all-targets --workspace.

Commit count: 3

cargo fmt