Trait port_expander::PortMutex
source · pub trait PortMutex {
type Port;
// Required methods
fn create(v: Self::Port) -> Self;
fn lock<R, F: FnOnce(&mut Self::Port) -> R>(&self, f: F) -> R;
}
Expand description
Common interface for mutex implementations.
port-expander
needs a mutex to ensure only a single pin object can access the port-expander at the same time,
in concurrent situations. port-expander
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:
Mutex | Feature Name | Notes |
---|---|---|
core::cell::RefCell | always available | For sharing within a single execution context. |
std::sync::Mutex | std | For platforms where std is available. |
critical_section::Mutex | critical-section | Use critical sections to ensure synchronized access, via the critical-section crate. |
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> port_expander::PortMutex for MyMutex<T> {
type Port = T;
fn create(v: T) -> Self {
Self(std::sync::Mutex::new(v))
}
fn lock<R, F: FnOnce(&mut Self::Port) -> R>(&self, f: F) -> R {
let mut v = self.0.lock().unwrap();
f(&mut v)
}
}
Required Associated Types§
Required Methods§
Object Safety§
This trait is not object safe.