jo

Crates.iojo
lib.rsjo
version0.1.0
sourcesrc
created_at2021-03-16 07:16:19.452898
updated_at2021-03-16 07:16:19.452898
descriptionJo is a Rust macro for ensuring non-parallel execution of functions.
homepage
repositoryhttps://github.com/yuulive/jo/
max_upload_size
id369553
size19,877
(yuulive)

documentation

https://docs.rs/jo/

README

jo

Jo is a procedural macro for Rust that allows you to ensure that functions are not running at the same time. This is especially useful for integration tests, where tests that are writing to the same database table should not run in parallel.

The goal of mutual exclusion is achieved by acquiring a mutex at the beginning of the annotated function. So in essence this macro is syntactical sugar for writing MUT.lock().unwrap() at the beginning of every function.

Different functions can synchronize on different mutexes. That's why a static mutex reference must be passed to the jo annotation.

Usage

use std::sync::Mutex;
use lazy_static::lazy_static;
use jo::jo;

// Create two locks
lazy_static! { static ref MUT_A: Mutex<()> = Mutex::new(()); }
lazy_static! { static ref MUT_B: Mutex<()> = Mutex::new(()); }

// Mutually exclude parallel runs of functions using those two locks

#[jo(MUT_A)]
fn function_a1() {
    // This will not run in parallel to function_a2
}

#[jo(MUT_A)]
fn function_a2() {
    // This will not run in parallel to function_a1
}

#[jo(MUT_B)]
fn function_b() {
    // This may run in parallel to function_a*
}

License

Licensed under either of

at your option.

Commit count: 7

cargo fmt