Trait flexible_locks::MutexProtected [] [src]

pub trait MutexProtected {
    type MutexType: RawMutex;
    type DataType: ?Sized;
    fn get_mutex(&self) -> &Self::MutexType;
fn get_mutex_mut(&mut self) -> &mut Self::MutexType;
fn get_data(&self) -> &Self::DataType;
fn get_data_mut(&mut self) -> &mut Self::DataType;
fn into_data(self) -> Self::DataType
    where
        Self::DataType: Sized
; }

A trait describing types that can be wrapped in a [Mutex].

It is not recommended to implement this trait manually. Instead, use the flexible-locks_derive crate that provides a custom #[derive(MutexProtected)].

When using that custom derive, the #[mutex] attribute is used to indicate the data field containing the raw mutex type.

Examples

extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::{Mutex, RawMutex};

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::CRITICAL_SECTION as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

#[derive(MutexProtected)]
struct Data {
    a: usize,
    #[mutex]
    mutex: RawOsMutex,
    b: usize,
}

Associated Types

Raw mutex pritimive used to protect the data type.

Data type that [Mutex::lock] will give access to.

[derive(MutexProtected)] makes this Self when the type is large, but when there are only two fields in the struct (whichever their order is), it will set the DataType to the type of the non-mutex field.

Examples

extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::{Mutex, RawMutex};

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::SRWLOCK as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

#[derive(MutexProtected, Default)]
struct Data {
    a: usize,
    b: usize,
    #[mutex]
    mutex: RawOsMutex,
}

#[derive(MutexProtected, Default)]
struct Data2 {
    a: usize,
    #[mutex]
    mutex: RawOsMutex,
}

#[derive(MutexProtected, Default)]
struct Data3 {
    #[mutex]
    mutex: RawOsMutex,
    a: usize,
}

fn main() {
    let mut mutex = Mutex::new(Data::default());
    mutex.lock().a = 10;
    let mut mutex = Mutex::new(Data2::default());
    *mutex.lock() = 10;
    let mut mutex = Mutex::new(Data3::default());
    *mutex.lock() = 10;
}

MutexWrap<RawOsMutex, usize> should be preferred to Mutex<Data2> and Mutex<Data3>.

Required Methods

Return an immutable reference to the raw mutex.

Return a mutable reference to the raw mutex.

Return an immutable reference to the data.

Return a mutable reference to the data.

Consumes the wrapping type, returning the underlying data.

Implementors