A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.rs/shared-bus/latest/shared_bus/trait.BusMutex.html below:

BusMutex in shared_bus - Rust

pub trait BusMutex {
    type Bus;

    // Required methods
    fn create(v: Self::Bus) -> Self;
    fn lock<R, F: FnOnce(&mut Self::Bus) -> R>(&self, f: F) -> R;
}
Expand description

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:

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 {
        Self(std::sync::Mutex::new(v))
    }

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

type BusManagerCustom<BUS> = shared_bus::BusManager<MyMutex<BUS>>;
Source

The actual bus that is wrapped inside this mutex.

Source

Create a new mutex of this type.

Source

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

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Sourceยง

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4