[][src]Crate mutex_trait

Low level definition of a Mutex.

This crate provides:

  • A Mutex trait that is to be used as the foundation of exclusive access to the data contained within it.
  • Helper traits and implementations which allows for multiple locks to be taken at once.

RFC that added this trait: RFC #377


use mutex_trait::prelude::*;

// A function taking 2 mutexes
fn normal_lock(
    a: &mut impl Mutex<Data = i32>,
    b: &mut impl Mutex<Data = i32>,
) {
    // Taking each lock separately
    a.lock(|a| {
        b.lock(|b| {
            *a += 1;
            *b += 1;

    // Or both at once
    (a, b).lock(|a, b| {
        *a += 1;
        *b += 1;

Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.31 and up. It might compile with older versions but that may change in any new patch release.



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.



Wraps a T and provides exclusive access via a Mutex impl.



Any object implementing this trait guarantees exclusive access to the data contained within the mutex for the duration of the lock.