Trait shared_bus::mutex::BusMutex[][src]

pub trait BusMutex<T> {
    fn create(v: T) -> Self;
fn lock<R, F: FnOnce(&T) -> R>(&self, f: F) -> R; }

An abstraction over a mutex lock.

Any type that can implement this trait can be used as a mutex for sharing a bus.

If the std feature is enabled, BusMutex is implemented for std::sync::Mutex. If the cortexm feature is enabled, BusMutex is implemented for [cortex_m::interrupt::Mutex].

If there is no feature available for your Mutex type, you have to write one yourself. It should look something like this (for cortex_m as an example):

extern crate cortex_m;

// You need a newtype because you can't implement foreign traits on
// foreign types.
struct MyMutex<T>(cortex_m::interrupt::Mutex<T>);

impl<T> shared_bus::BusMutex<T> for MyMutex<T> {
    fn create(v: T) -> MyMutex<T> {

    fn lock<R, F: FnOnce(&T) -> R>(&self, f: F) -> R {
        cortex_m::interrupt::free(|cs| {
            let v = self.0.borrow(cs);

type MyBusManager<L, P> = shared_bus::BusManager<MyMutex<L>, P>;

Required Methods

Create a new instance of this mutex type containing the value v.

Lock the mutex for the duration of the closure f.

Implementations on Foreign Types

impl<T> BusMutex<T> for Mutex<T>

impl<T> BusMutex<T> for Mutex<T>