MutexProtected

Trait MutexProtected 

Source
pub trait MutexProtected {
    type MutexType: RawMutex;
    type DataType: ?Sized;

    // Required methods
    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;
}
Expand description

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,
}

Required Associated Types§

Source

type MutexType: RawMutex

Raw mutex pritimive used to protect the data type.

Source

type DataType: ?Sized

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§

Source

fn get_mutex(&self) -> &Self::MutexType

Return an immutable reference to the raw mutex.

Source

fn get_mutex_mut(&mut self) -> &mut Self::MutexType

Return a mutable reference to the raw mutex.

Source

fn get_data(&self) -> &Self::DataType

Return an immutable reference to the data.

Source

fn get_data_mut(&mut self) -> &mut Self::DataType

Return a mutable reference to the data.

Source

fn into_data(self) -> Self::DataType
where Self::DataType: Sized,

Consumes the wrapping type, returning the underlying data.

Implementors§