mutex_trait

Module prelude

Source
Expand description

Makes locks work on N-tuples, locks the mutexes from left-to-right in the tuple. These are used to reduce rightward drift in code and to help make intentions clearer.

§Example

use mutex_trait::prelude::*;

fn normal_lock(
    a: &mut impl Mutex<Data = i32>,
    b: &mut impl Mutex<Data = i32>,
    c: &mut impl Mutex<Data = i32>
) {
    // A lot of rightward drift...
    a.lock(|a| {
        b.lock(|b| {
            c.lock(|c| {
                *a += 1;
                *b += 1;
                *c += 1;
            });
        });
    });
}

Has a shorthand as:

use mutex_trait::prelude::*;

fn tuple_lock(
    a: &mut impl Mutex<Data = i32>,
    b: &mut impl Mutex<Data = i32>,
    c: &mut impl Mutex<Data = i32>
) {
    // Look! Single indent and less to write
    (a, b, c).lock(|a, b, c| {
        *a += 1;
        *b += 1;
        *c += 1;
    });
}

Re-exports§

Traits§