pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
Mutex protecting a value of type T. std::sync::Mutex<T> /
parking_lot::Mutex<T> analog.
The lock and the data live together in one type. Acquiring the
lock via Mutex::lock returns a MutexGuard<T> that
Derefs to T, so the only way to touch the data is to hold
the lock first — the borrow checker enforces the contract that
lock then *g = … is the only legal access pattern.
§Differences from std::sync::Mutex
- No poisoning.
lock()returnsResult<MutexGuard<T>, Error>, not std’sLockResult<MutexGuard<T>>. Mirrors parking_lot’s choice — forcing callers to unwrap a never-firingPoisonErroris worse ergonomics than reporting only the backend errors that can actually occur. try_lock_for/try_lock_untilvariants for bounded waits. Parking_lot has these; std does not. ReturnResult<MutexGuard, Error>(withError::Timeouton deadline elapsed), not parking_lot’sOption<MutexGuard>— we have backend errors that aren’t simply “could not acquire”.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn new(val: T) -> Result<Self>
pub fn new(val: T) -> Result<Self>
Create a new mutex around val via heap allocation (heap mode only).
Sourcepub fn create(storage: &'static MutexStorage, val: T) -> Result<Self>
pub fn create(storage: &'static MutexStorage, val: T) -> Result<Self>
Create a mutex around val that works in both heap and zero-heap
modes: heap mode ignores storage and allocates; zero-heap mode backs
the handle with the caller-provided storage (which must outlive the
Mutex). This is the mode-agnostic constructor downstream crates use
— new/from_static are gated to one mode and from_static needs the
crate-private storage type, so neither is callable portably.
Sourcepub fn into_inner(self) -> Result<T>
pub fn into_inner(self) -> Result<T>
Consume the mutex and return the protected value.
Available in heap mode only (zero-heap mode lacks a destroy path
for the handle — the handle’s storage outlives the Mutex
anyway).
Source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Get a mutable reference to the protected value without locking.
Safe because &mut self proves the caller has exclusive access
— no other thread can hold a guard at this moment. Matches
std::sync::Mutex::get_mut.
Sourcepub fn lock(&self) -> Result<MutexGuard<'_, T>>
pub fn lock(&self) -> Result<MutexGuard<'_, T>>
Acquire the mutex, blocking indefinitely. Returns an RAII guard that releases the lock on drop.
§Errors
Returns the substrate’s error code if the mutex handle is invalid (programming error — same failure mode as in C/C++).
Sourcepub fn try_lock(&self) -> Result<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Result<MutexGuard<'_, T>>
Attempt to acquire the mutex without blocking.
std::sync::Mutex::try_lock analog.
§Errors
Returns Error::WouldBlock if the mutex is currently held by
another thread.
Sourcepub fn try_lock_for(&self, d: Duration) -> Result<MutexGuard<'_, T>>
pub fn try_lock_for(&self, d: Duration) -> Result<MutexGuard<'_, T>>
Attempt to acquire the mutex, waiting up to d.
parking_lot::Mutex::try_lock_for analog.
§Errors
Returns Error::Timeout if the lock cannot be acquired within
the duration.
Sourcepub fn try_lock_until(&self, deadline: Instant) -> Result<MutexGuard<'_, T>>
pub fn try_lock_until(&self, deadline: Instant) -> Result<MutexGuard<'_, T>>
Attempt to acquire the mutex by the given deadline.
parking_lot::Mutex::try_lock_until analog. Use
Instant::FOREVER for an
indefinite wait.
§Errors
Returns Error::Timeout if the deadline elapses before the lock
is acquired.