Trait shared_bus::BusMutex[][src]

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

Common interface for mutex implementations.

shared-bus needs a mutex to ensure only a single device can access the bus at the same time in concurrent situations. shared-bus already implements this trait for a number of existing mutex types. Most of them are guarded by a feature that needs to be enabled. Here is an overview:

MutexFeature NameNotes
shared_bus::NullMutexalways availableFor sharing within a single execution context.
std::sync::MutexstdFor platforms where std is available.
cortex_m::interrupt::Mutexcortex-mFor Cortex-M platforms; Uses a critcal section (i.e. turns off interrupts during bus transactions).

For other mutex types, a custom implementation is needed. Due to the orphan rule, it might be necessary to wrap it in a newtype. As an example, this is what such a custom implementation might look like:

struct MyMutex<T>(std::sync::Mutex<T>);

impl<T> shared_bus::BusMutex for MyMutex<T> {
    type Bus = T;

    fn create(v: T) -> Self {

    fn lock<R, F: FnOnce(&mut Self::Bus) -> R>(&self, f: F) -> R {
        let mut v = self.0.lock().unwrap();
        f(&mut v)

// It is also beneficial to define a type alias for the BusManager
type BusManagerCustom<BUS> = shared_bus::BusManager<MyMutex<BUS>>;

Associated Types

type Bus[src]

The actual bus that is wrapped inside this mutex.

Loading content...

Required methods

fn create(v: Self::Bus) -> Self[src]

Create a new mutex of this type.

fn lock<R, F: FnOnce(&mut Self::Bus) -> R>(&self, f: F) -> R[src]

Lock the mutex and give a closure access to the bus inside.

Loading content...

Implementations on Foreign Types

impl<T> BusMutex for Mutex<T>[src]

type Bus = T

Loading content...


impl<BUS> BusMutex for AtomicCheckMutex<BUS>[src]

type Bus = BUS

impl<T> BusMutex for NullMutex<T>[src]

type Bus = T

impl<T> BusMutex for CortexMMutex<T>[src]

type Bus = T

Loading content...